如何为通知设置点击监听器?

32

我正在使用以下代码通过AlarmManager启动服务时发出通知:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
    "Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);

如何为此项设置意图,以便当用户单击它时,它会启动某个活动?

3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
31

关于yoshi24的评论,您可以像这样设置额外内容。

final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("key", "value");
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

在使用pending intents之前,您需要注意这一点。

https://dev59.com/_nM_5IYBdhLWcg3w2XHw

更新:像这样的代码可以为您解决问题:

int your mainfest

<activity android:name=".MyActivity" android:launchMode="singleTop" ... />

在你的活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    processIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {     
    processIntent(intent);
};

private void processIntent(Intent intent){
    //get your extras
}

你贴的链接是关于什么的?它和我正在尝试做的事情有关系吗? - yoshi24
1
你需要在清单文件中将意图标志更改为 **android:launchMode="singleTask"**,并在活动内重写 onNewIntent(Intent intent) 方法。这样可以使你接收到参数。 - Samuel
1
@yoshi24:能否也选择我的回复作为答案?我猜我们可以选择多个,但我不知道是否可行。当然,如果对你有用的话。 - Samuel
1
@yoshi24:我不是想要拿走,我只是想标记两个回复作为答案,就像打勾一样。看起来像复选框。;) - Samuel
1
@yoshi24:我在我的一个问题中测试了它,结果它切换了 - Samuel
显示剩余3条评论

26

你需要将Activity类作为Intent的一部分放入PendingIntent中。目前你的Intent是空的。要重定向到新的Activity,应该这样做:

// This line of yours should contain the activity that you want to launch. 
// You are currently just passing empty new Intent()
PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);


你能提供一个例子吗? - yoshi24
还有一件事不在问题范围内,我能否设置一个额外的参数来传递它? - yoshi24
@yoshi24:我猜你可以将一个 bundle 添加到 intent 中。 - Samuel
我自己还没有尝试过,但是这篇帖子似乎回答了你的问题https://dev59.com/_nM_5IYBdhLWcg3w2XHw - momo
@yoshi24:哎呀,我想你是在我打字的时候发帖了。 :) - Samuel

10

我做到了,

  • 我对新的意图添加了 Intent.FLAG_ACTIVITY_CLEAR_TOP 标记

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    Intent intent = new Intent(this, NoficationDemoActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Bundle bundle = new Bundle();
    bundle.putString("buzz", "buzz");
    intent.putExtras(bundle);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);
    notification.number += 1;
    notificationManager.notify(0, notification);
    
  • 在onCreate方法中我这样做:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if(getIntent().getExtras()!=null){
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
    }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,