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