gpt4 book ai didi

android - onClick() 监听通知

转载 作者:行者123 更新时间:2023-11-29 00:04:02 24 4
gpt4 key购买 nike

我想在单击通知时突出显示 ListItem。我显示 ListView 的 Activity 已经打开,所以我无法在点击通知时再次打开它。我搜索了很多,但我认为没有任何 onClick() 通知方法。那么请告诉我该怎么做?

这就是我生成通知的方式

Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, notification_id, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setTicker("Smart Locator");
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle(name);
DetailsContainer dc = new LocationDetails(context).getDetails(location);
mBuilder.setContentText(date + ", " + dc.area + " " + dc.locality);
mBuilder.setContentIntent(pIntent).getNotification();
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(document_id, notification_id, mBuilder.build());

最佳答案

您可以在未决 Intent 中添加额外内容。

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

然后,当您的 Activity “重新启动”时,您可以通过收到的 bundle 检查通知是否被点击。

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

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

private void processIntent(Intent intent){
//get your extras
//if clicked, do something with ListView
}

检查 this question了解更多。

关于android - onClick() 监听通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com