PendingIntent继续激活活动重新加载

时间:2011-07-10 23:46:15

标签: android android-activity reload android-pendingintent

我点击了一个基本上向我的主菜单发送意图的通知的PendingIntent触发(设置为singleTop启动模式)。这将触发onNewIntent,然后加载我想要的数据并转到另一个加载了该数据的活动。不幸的是,如果应用程序当前关闭,onNewIntent不起作用,所以我不得不从onCreate调用onNewIntent并包含一个check in thenNewIntent。

除了现在你发出通知后,一切都工作得非常好,pendingIntent似乎对每次调用onCreate被调用感到满意,它不会消失。我不确定在使用后如何检查或甚至清除待处理的意图。我的代码如下:

protected void onNewIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    if(extras != null && extras.containsKey("notification")) {
        if(extras.getBoolean("notification", false)) {
            loadData(extras.getString("data"));
            Intent myIntent = new Intent(this, OtherClass.class);
            startActivity(myIntent);
        }
    }
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    onNewIntent(getIntent());
}

//Code snippet of notification setup
notification = new Notification(R.drawable.icon, "Notification Reminder", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, MainMenu.class);

Bundle bundle = new Bundle();
contentTitle = "Notification_Title";
bundle.putBoolean("notification", true);
bundle.putString("data", contentTitle.toString());
notificationIntent.putExtras(bundle);
                            notificationIntent.setData((Uri.parse("application://"+SystemClock.elapsedRealtime())));

contentTitle = contentTitle + " click me!";
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, notificationText, contentIntent);

1 个答案:

答案 0 :(得分:2)

只需使用PendingIntent.FLAG_ONE_SHOT标志:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);