【问题标题】:Trying to handle event triggered by Service in the Activity试图在 Activity 中处理 Service 触发的事件
【发布时间】:2015-04-24 18:19:30
【问题描述】:

我有一个后台 Service,它可以触发事件并为我的应用构建通知。如果单击,通知应打开MainActivity,并根据发送的信息,在此Activity 中打开我的片段之一。为此,Notification 包含一个 PendingIntent,它将数据保存到 Intent 的 Bundle

这个触发事件有三种场景:

  1. 应用程序在前台:我的服务发送一个Broadcast 和我的BroadcastReceiverActivity 获取事件并处理它。不需要Notification。效果很好。
  2. 应用被杀:PendindIntent 重新打开我的应用,我的Activity 使用getIntent().getExtras() 通过Bundle 访问我的信息。一切正常。
  3. 后台应用程序:Activity 已经创建,因此广播接收器已注册。我点击我的通知,但没有任何反应。我既没有收到广播消息,也无法访问我的Bundle(使用getIntent().getExtras() 在onResume 中检查它,它是空的)。

关于如何解决第三种情况有什么想法吗?

创建通知的代码:

private void createNotification() {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.my_icon);
    mBuilder.setContentTitle("My App");
    mBuilder.setContentText("Notification message");

    Bundle bundle = new Bundle();
    bundle.putString(MainActivity.OPEN_FRAGMENT, "myFragment");
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.putExtra("myInfo","myInfo");
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    resultIntent.putExtras(bundle);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}

【问题讨论】:

    标签: android service broadcastreceiver bundle android-pendingintent


    【解决方案1】:

    你有没有尝试过改变

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
    

    【讨论】:

    • 我之前有过这段代码,后来改成了现在的。在实践中它没有任何区别。我注意到的是,一旦我从通知中打开我的(被杀死的)应用程序,它就会在 Bundle 中包含信息。如果我将它发送到后台并带到前台,信息仍然存在。即使我阅读它并执行 Bundle.clear,下次我将其带到前台时,它也会在 Bundle 中包含信息。
    • 我可以,但实际上与我的 BroadcastReceiver 没有任何关系(我正在动态创建和注册)。根据这篇文章:stackoverflow.com/questions/23154922/…我可能必须将它注册为一个单独的类并在 Manifest 中声明它。
    • 好的,但是您是否在清单中将您的活动注册为单个实例?这也可能导致问题。
    • 设置为 singleTop。我改变了它,但得到了相同的结果。我实际上发现getIntent().getExtras().clear() 不起作用,所以我做了getIntent.removeExtra()。现在我确定它只会在它被杀死时从 Notification 中接收 Bundle。很奇怪。
    【解决方案2】:

    终于找到了解决办法!

    我的情况是这样的:

    1. 应用程序前台:服务发送广播。 Activity 接收它并处理事件。
    2. 应用程序被杀:点击通知,PendingIntent 启动。应用重新打开并使用来自 Bundle 的信息处理事件。
    3. 后台应用程序:点击通知,它发送一个 PendingIntent,但是 Activity 已经在运行,所以它不会覆盖它,这就是为什么什么也没发生。

    解决方案:重写 onNewIntent 方法:

    @Override
    protected void onNewIntent(Intent intent) { // Receives the PendingIntent
        super.onNewIntent(intent);
        if (intent.getExtras() != null && intent.getExtras().getString(OPEN_MY_FRAGMENT) != null) {
            // TODO something with intent.getExtras().getString("myInfo");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2022-11-22
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2018-07-19
      • 2011-02-27
      相关资源
      最近更新 更多