Android OnNewIntent未被调用。

18

我尝试了多种方法,但都不起作用。我不知道为什么这么复杂,在文档中看起来很容易! 我想通过通知(用户在通知栏中点击它)触发OnNewIntent。

目前,我已将我的Activity设置为singleTop。

<activity
    android:name="-.-.-.MainMenuActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" >
</activity>

这是我创建通知的代码:

public void showNotification(String text, String componentId){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")   
            .setAutoCancel(true)
            .setContentText(text);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainMenuActivity.class);
    if(!componentId.equalsIgnoreCase("")){                         
        resultIntent.putExtra("componentId", componentId);
    }   
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
    mBuilder.setFullScreenIntent(resultPendingIntent, false);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(0, mBuilder.build());
}

这是我MainMenuActivity中的OnNewIntent方法:

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
    setIntent(intent);

    ...
}

我从来没有收到OnNewIntent的调用。我不知道我做错了什么。整个应用程序中我只使用了2个活动,MainMenuActivity在LoginActivity之后,因此MainMenuActivity应该始终位于堆栈顶部(我有更多片段,在其中我替换了MainMenuActivity内部的片段)。

任何帮助都将不胜感激!谢谢大家。


1
你有没有在这方面取得任何进展?我基本上处于同样的情况。 - Piwaf
3个回答

13

首先,您应该像下面这样在清单文件中的活动定义中添加android:launchMode ="singleTop"

<activity
    android:name="MainActivity"
    android:launchMode="singleTop"
</activity>

就像 Hasan Masud 所说,你应该像下面一样将 Intent.ACTION_MAINIntent.CATEGORY_LAUNCHER 添加到你的动作中。

Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

就是这样。通过这种方式,如果应用程序处于打开状态并且您单击通知,则onNewIntent(..)方法将触发。但无论发生什么情况,如果应用程序关闭,在单击通知时,通知意图将转到当前 Activity 的 onCreate(..)方法。


1
你是最棒的!我花了很长时间才找到需要添加 Intent.ACTION_MAIN 和 INTENT.CATEGORY_LAUNCHER!!! 我在任何地方都没有看到过它的写法... - Mars

13

我发布问题后很快就解决了。 我认为我们代码中的关键区别在于,我将标志“PendingIntent.FLAG_UPDATE_CURRENT”传递给PendingIntent对象的创建/检索。这篇帖子帮助我找到了答案。

Notification.Builder mBuilder =
                new Notification.Builder(context)
                .setSmallIcon(R.drawable.notifyicon)
                .setContentTitle(title)
                .setContentText(extras.getString("message"))
                .setAutoCancel(true)
                .setOnlyAlertOnce(false)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[] {0,500,250,500});

        if(badgeCount > 1)
            mBuilder.setNumber(badgeCount);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, SiteViewActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.putExtra(NOTIFY_INTENT_TYPE_KEY, alertType);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, alertType, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(alertType, mBuilder.build());

还有一个问题,您可以添加“alertType”值 - 这是解决我的同样问题的关键!!谢谢! - alena_fox_spb

0
经过长时间的检查,我注意到Android 4.4(Kitkat和可能更低版本)如果您在pendingIntent上使用FLAG_UPDATE_CURRENT,则不会调用onNewIntent和onResume。一旦将其更改为FLAG_ONE_SHOT,它就会开始工作。但是,在Android 6(可能也是5)上,即使使用FLAG_UPDATE_CURRENT标志,onNewIntent也可以正常工作。
然而,如果您创建多个挂起的意图(例如在接收两个通知后),则使用FLAG_ONE_SHOT标志不会更新数据,因此FLAG_CANCEL_CURRENT标志可能是更好的选择(它不会出现未更新数据的问题)。

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