首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有时不会触发onNewIntent()

有时不会触发onNewIntent()
EN

Stack Overflow用户
提问于 2013-03-27 06:07:37
回答 1查看 543关注 0票数 0

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

代码语言:javascript
复制
@Override
    public void onNewIntent(Intent intent)  //when notification is clicked
    {
        Log.i("mydebug","Notification clicked.");

    }

这里是创建通知的位置:

代码语言:javascript
复制
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());
EN

回答 1

Stack Overflow用户

发布于 2013-03-27 08:48:18

尝试将标志设置为:

代码语言:javascript
复制
FLAG_ACTIVITY_CLEAR_TOP

而不是

代码语言:javascript
复制
FLAG_ACTIVITY_SINGLE_TOP
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15648309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档