通知

时间:2016-05-21 13:10:53

标签: android

我希望在点击通知时突出显示ListItem。显示ListView的我的活动已经打开,因此我无法再点击通知再次打开它。我搜索了很多,但我认为没有任何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());

2 个答案:

答案 0 :(得分:3)

您可以在待处理的意图中添加额外费用。

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

然后,当您的活动“重新启动”时,您可以通过收到的捆绑包检查是否已点击通知。

@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了解更多信息。

答案 1 :(得分:-1)

在待处理的意图中添加额外内容:

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

在您的活动中使用以下代码:

@Override
protected void onNewIntent(Intent intent) {
    try {
        //on click notification
        Bundle extras = intent.getExtras();
        if (extras.getBoolean("NotClick")) {
             //Do your stuff here 
        }
    } catch (Exception e) {
        Log.e("onclick", "Exception onclick" + e);
    }
}