有时onNewIntent()不会被触发

时间:2013-03-26 22:07:37

标签: android android-intent

我已经在我的应用程序设置了通知的位置,当它被点击时,我想知道在程序中。每次单击它时,主活动都会打开但不时会遇到一个错误,当点击通知时,不会调用onNewIntent(Intent intent)。这是我的onNewIntent:

@Override
    public void onNewIntent(Intent intent)  //when notification is clicked
    {
        Log.i("mydebug","Notification clicked.");

    }

这里是创建通知的地方:

Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setWhen(System.currentTimeMillis())
                     .setContentTitle(schedname)
                     .setContentText("Click to deactivate this silence schedule.")
                     .setContentIntent(pendingIntent)
                     .setSmallIcon(R.drawable.ic_launcher);
                    //notification.flags |= Notification.FLAG_NO_CLEAR; //Notification cannot be clearned by user
            NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            // Uses the default lighting scheme
            builder.build().defaults |= Notification.DEFAULT_LIGHTS;
            builder.setAutoCancel(true);
            builder.setOngoing(true);
            builder.setContentIntent(pendingIntent);

            // Will show lights and make the notification disappear when the presses it
            builder.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
            mNotificationManager.notify(1, builder.build());

1 个答案:

答案 0 :(得分:1)

尝试将标志设置为:

FLAG_ACTIVITY_CLEAR_TOP

而不是

FLAG_ACTIVITY_SINGLE_TOP