Android的通知操作,不会触发(PendingIntent) [英] Android Notification Action is not fired (PendingIntent)
问题描述
我想加在我的应用程序,它是一个音乐播放器的通知操作的项目。流时启动通知应当触发和停止按钮的流应该被显示在notfication。通知做工精细,到目前为止,我有停止操作项目的麻烦。下面是它是如何在服务宣布启动流:
I am trying to add an Notification action item in my app which is a music player. When a stream is started a notification should be triggered and an stop button for the stream should be displayed in the notfication. The notification working fine so far, I am having trouble with the stop action item. Here is how it is declared in the service starting the stream:
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);
现在在onResume() - 我的活动方法,我请与getIntent()getStringExtra()的STOP多余的,但我通过getIntent()检索的意图已经无需额外设置:(
Now in the onResume()-method of my activity I check with getIntent().getStringExtra() for the "STOP" extra, but the intent I retrieved via getIntent() has no extras set :(
我也试过检查发送一个广播(我有一个广播接收器工作从服务活动进行沟通)
I also tried to check to send an broadcast (i have a broadcast receiver working to communicate from the service to the activity)
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);
现在这个工作,如果该活动是目前在前台。如果活动是在后台停止按钮不执行任何操作:(
Now this works if the activity is currently in the foreground. If the activity is in the background the stop button does nothing :(
编辑: 我有BroadcastReceiver的在我的活动作为一个私有类
I have the BroadcastReceiver in my Activity as a private class
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
..
}}
在onResume()注册我的应用程序该接收器:
In the onResume() register my app for this receiver:
intentFilter = new IntentFilter(STOP_MEDIAPLAYER);
registerReceiver(dataUpdateReceiver, intentFilter);
在onPause()
onPause()
unregisterReceiver(dataUpdateReceiver);
现在,如果我从的onPause()除去注销 - 方法在接收到的广播,即使该应用/活动不是在前台了。但是,这是正确的方式做到这一点?我得到这个注册/注销-东西从web我觉得一个教程。
Now if I remove the unregistering from the onPause()-method the broadcast is received even if the app/activity is not in the foreground anymore. But is this the right way to do it? I got this register/unregister-stuff from a tutorial on the web i think..
推荐答案
我找到解决办法在此线程对谷歌code <一个href="https://$c$c.google.com/p/android/issues/detail?id=61850">https://$c$c.google.com/p/android/issues/detail?id=61850
I find solution in this thread on google code https://code.google.com/p/android/issues/detail?id=61850
要解决它,你必须添加PendingIntent.FLAG_CANCEL_CURRENT标志你PendingIntent。
To fix it you must add PendingIntent.FLAG_CANCEL_CURRENT flag to you PendingIntent.
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
stopIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
这篇关于Android的通知操作,不会触发(PendingIntent)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!