未触发Android通知操作(PendingIntent)

MrH*_*ill 17 android android-intent android-notifications android-activity android-pendingintent

我想在我的应用程序中添加一个Notification操作项,这是一个音乐播放器.启动流时,应触发通知,并在通知中显示流的停止按钮.到目前为止通知工作正常,我遇到了停止操作项目的问题.以下是在启动流的服务中声明它的方式:

Intent stopIntent = new Intent(this, MusicPlayerNew.class);
stopIntent.putExtra("STOP", "STOP");
PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0,
                stopIntent, PendingIntent.FLAG_UPDATE_CURRENT, null);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent);
Run Code Online (Sandbox Code Playgroud)

现在在onResume() - 我的活动方法中,我用getIntent().getStringExtra()检查"STOP"额外,但我通过getIntent()检索的意图没有设置额外的东西:(

我还试图检查发送广播(我有一个广播接收器工作从服务到活动进行通信)

Intent stopIntent2 = new Intent(MusicPlayerNew.STOP_MEDIAPLAYER);
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
                stopIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent2); 
Run Code Online (Sandbox Code Playgroud)

现在,如果活动当前位于前台,则此功能正常.如果活动在后台,则停止按钮不执行任何操作:(

编辑: 我在我的Activity中将BroadcastReceiver作为私有类

private class DataUpdateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
..
}}
Run Code Online (Sandbox Code Playgroud)

在onResume()中注册我的app接收器:

intentFilter = new IntentFilter(STOP_MEDIAPLAYER);
registerReceiver(dataUpdateReceiver, intentFilter);
Run Code Online (Sandbox Code Playgroud)

的onPause()

unregisterReceiver(dataUpdateReceiver);
Run Code Online (Sandbox Code Playgroud)

现在,如果我从onPause() - 方法中删除取消注册,即使app/activity不在前台,也会收到广播.但这是正确的方法吗?我从网上的教程中得到了这个注册/取消注册的内容我认为..

Aym*_*bsi 11

这是非常晚的答案,但它可能会帮助某人:

您应该根据要运行的意图选择正确的Pending intent.这里有些例子:

对于活动使用如下:

Intent i = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Run Code Online (Sandbox Code Playgroud)

对于以下服务使用:

Intent i = new Intent(this, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
Run Code Online (Sandbox Code Playgroud)

对于广播接收器使用如下:

Intent i = new Intent(this, YourReciver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
Run Code Online (Sandbox Code Playgroud)

如果需要,您可能需要更改请求代码和标志


Nik*_*Nik 10

我在谷歌代码的这个帖子中找到了解决方案https://code.google.com/p/android/issues/detail?id=61850

要解决它,你必须添加PendingIntent.FLAG_CANCEL_CURRENT标志到你的PendingIntent.

PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
            stopIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)


Ate*_*can 6

我今天遇到了这个问题.在我的情况下,它使用了前一个intent实例的缓存intent extras,因为pendingIntent构造函数的所有参数都是相同的.我找到了两个解决方案......

  1. 使用Nik提到的FLAG_CANCEL_CURRENT .
  2. 传递一个独特requestCodependingIntent如下

    PendingIntent pi = PendingIntent.getService(this, UNIQUE_ID, pi, 0);
    
    Run Code Online (Sandbox Code Playgroud)

在我的情况下,第二种方法解决了问题,因为我需要保持以前的通知存活.可能这会帮助有类似问题的人.


Dan*_*gle 5

我今天遇到了这个问题,这是由于活动未被注册或添加到AndroidManifest.xml引起的.我以为我在那里有它,但事实并非如此.此外,尝试以其意图调用操作时,未记录任何错误.

我通过创建一个intent然后在不使用通知的情况下调用startAcitivty(intent)来解决这个问题.然后它给了我一个错误,说明清单中可能缺少活动.

如果其他答案都没有解决您的问题,那么希望这样做.通常棘手的问题是简单而愚蠢的结果.