【问题标题】:PendingIntent continues to fire on activity reloadPendingIntent 继续在活动重新加载时触发
【发布时间】:2011-10-02 10:30:39
【问题描述】:

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

除了现在在您触发通知之后,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);

【问题讨论】:

  • 我通过向我的 MainMenu 启动一个新 Intent 来解决此问题。这似乎有点骇人听闻,但它适用于我想要的活动,加载我希望的数据,并在用户使用后退按钮返回主菜单时将其保持打开状态。我修改后的 onNewIntent 只有一个“startActivity(new Intent(this, MainMenu.class));”就在 startActivity(myIntent); 之前。

标签: android android-activity reload android-pendingintent


【解决方案1】:

只需像这样使用 PendingIntent.FLAG_ONE_SHOT 标志:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    相关资源
    最近更新 更多