0
你好,因为标题说..我需要发送一个动作,从通知广播接收器...我做了大量的研究,找到一种方法来解决它..但我没有得到对广播接收机采取的行动请帮助我一点,对于我造成的任何不便,我深表歉意。通知发送intent to broadcastreciever
通知代码:
@Override
public void onPause() {
Intent resultIntent = new Intent(getApplicationContext(), AndroidPlayer.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent("ovh.smonitor.androidplayer.ACTION_PLAY");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent stopIntent = new Intent("ovh.smonitor.androidplayer.ACTION_STOP");
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder = new Notification.Builder(this)
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText("Press to return.")
.setSmallIcon(R.drawable.ic_radio_white)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_play_arrow_white, "Play", playPendingIntent)
.addAction(R.drawable.ic_pause_white, "Stop", stopPendingIntent)
.setContentIntent(resultPendingIntent)
.setOngoing(true)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
if (this.isFinishing())
notificationManager.cancel(0);
super.onPause();
}
广播接收器:
package ovh.smonitor.androidplayer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
public class RemoteControlReceiver extends BroadcastReceiver {
MediaPlayer mPlayer = new MediaPlayer();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("ovh.smonitor.androidplayer.ACTION_PLAY")) {
System.out.println("ACTION PLAY !.");
} else if (intent.getAction().equals("ovh.smonitor.androidplayer.ACTION_STOP")) {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
}
}
}
清单(INSIDE应用):
<receiver
android:name=".RemoteControlReceiver"
android:enabled="true">
<intent-filter>
<action android:name="ovh.smonitor.androidplayer.ACTION_PLAY" />
<action android:name="ovh.smonitor.androidplayer.ACTION_STOP" />
</intent-filter>
</receiver>
为什么我没有得到我的接收机任何回答任何建议。为你的时间thnx!
日Thnx您的时间..已经发现,从广播获取意图..我的问题是..我不能使用已经发挥的主要活动的MediaPlayer对象的方式..所以我可以阻止它,并从通知重播t错误,媒体播放器为空...所以我不能停止与通知栏只与主要活动的应用程序..你能帮助我吗? –
我认为你可以使用**动态接收器**而不是**静态接收器**:在你的主要活动中创建你的接收器,然后当你接收到一些动作时你可以得到mediaplayer对象。并且自你的上一个问题已经解决以来,你最好更新你的问题,让别人知道你目前的问题。 – starkshang