通知向broadcastreciever发送意图

时间:2015-11-15 10:58:16

标签: android android-intent

你好,因为标题说..我需要从通知发送一个动作广播接收器...我做了很多研究找到一种方法来解决它..但我没有得到广播接收器上的动作请帮助我一点对不起给我带来的任何不便......!。

通知代码:

@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();
            }
        }
    }
}

清单(内部申请):

<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你的时间!。

1 个答案:

答案 0 :(得分:0)

您向IntentFilter标记添加了两个操作,这意味着只有两个操作同时发送,您的接收者可以接收操作并调用OnReceive,所以要修复它,就像这样: 替换

 <intent-filter>
     <action android:name="ovh.smonitor.androidplayer.ACTION_PLAY" />
     <action android:name="ovh.smonitor.androidplayer.ACTION_STOP" />
 </intent-filter>

  <intent-filter>
     <action android:name="ovh.smonitor.androidplayer.ACTION_PLAY" />
  </intent-filter>
  <intent-filter>
     <action android:name="ovh.smonitor.androidplayer.ACTION_STOP" />
  </intent-filter>