Android BroadcastReceiver无法接收sendOrderedBroadcast

时间:2014-12-19 08:52:06

标签: android broadcastreceiver foreground-service

我有一个活动MyActivity启动Foreground服务播放音乐,一旦音乐完成,服务通过发送sendOrderedBroadcast

来暗示
  public class MyActivity  extends Activity {  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      .....
      Intent intent = new Intent(MyActivity  .this, MyMusicPlayerService.class);
      startService(intent);
     }

    @Override
    protected void onResume() {
      super.onResume();
      IntentFilter from_sendorderedNotification = new IntentFilter(ACTION_SESSION_COMPLETE);
      registerReceiver(notificationsessionComplted, from_sendorderedNotification);

    }


    BroadcastReceiver notificationsessionComplted = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("xa", "onRecive of activity Send Boarcast by order ****************");

    }
  };
}

MyMusicPlayerService.java

   public class MyMusicPlayerServiceextends Service {
     public static final String ACTION_SESSION_COMPLETE = "my.action.COMPLETE";

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
       ......
       ....Start mediaplayer and set setOnCompletionListener....
       return START_STICKY;
      }

    public void onCompletion(MediaPlayer _mediaPlayer){
      stopForeground(true);
      stopSelf();

      Intent i = new Intent();
      i.setAction(ACTION_SESSION_COMPLETE);
      MyMusicPlayerServiceextends .this.sendOrderedBroadcast(i, null);
   }
 }

SendNotification .java

 public class SendNotification extends BroadcastReceiver{

   @Override
   public void onReceive(Context arg0, Intent arg1) {
      Notification notification = ....
      notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
      Intent intent = new Intent(arg0, MyActivity  .class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0, intent, 0);
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
      final int _ID = 10;
      mNotificationManager.notify(_ID, notification);

}

Mainfest文件

 <service android:name="com.example.sendbroadcast.MyMusicPlayerService" />

     <activity
        android:name="com.example.sendbroadcast.MyActivity"
       android:launchMode="singleTop"
        android:noHistory="true">
      <intent-filter android:priority="1" >
            <action android:name="my.action.COMPLETE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

      <receiver android:name="com.example.sendbroadcast.SendNotification" >
        <intent-filter android:priority="2" >
            <action android:name="my.action.COMPLETE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

我在MyActivity的onPause中取消注册接收器,当Activity处于onStop状态时,接收者SendNotification将接收广播并通过通知亲密用户并且点击它将带回MyActivity.Suppose Myactivity已经破坏并且从堆栈中删除没有广播发送或接收。我知道为什么接收器在这种情况下无法接收。

0 个答案:

没有答案