2017-08-07 9 views
-1

サービスからアクティビティへのダイナミックブロードキャストを送信しようとしています。 Notification.classサービスからブロードキャストを送信すると、そのブロードキャストはアクティビティによって受信されます。しかし、sendData.classサービスからブロードキャストを送信しようとすると、そのデータはアクティビティで受信されません。私は何が起こったのか分からない。 これが私の活動のコードである: -なぜBroadcastReceiverがAndroid以外の状態になっていないのか

 protected void onCreate(Bundle savedInstanceState) { 
      . 
      . 
      . 
      mIntentFilter = new IntentFilter(); 

      mIntentFilter.addAction(mBroadcastStringAction); 

      . 
      . 
      . 
      . 
      . 


     } 


     private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
      @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) 
      @Override 
      public void onReceive(Context context, Intent intent) { 
// This Broadcast is called by getData.class service and not working :(
       if(intent.getAction().equals(PLAY_MUSIC)){ 

        Toast.makeText(context, "Broadcast Recieved", Toast.LENGTH_SHORT).show(); //Not showing Toast message also 
        String action = intent.getStringExtra("play"); 
        if(action.equals("true")) { 
         playBtnClick(); 
        } 
       } 

//This Broadcast is Called by Notification.class & Working Properly 
       if (intent.getAction().equals(mBroadcastStringAction)) { 
        int position = intent.getIntExtra("position", 0); 
        int duration = intent.getIntExtra("duration", 0); 
        sb.setMax(duration); 
        sb.setProgress(position); 
        setSongDetails(); 

       } 
      } //I have also try else if condition also 
     }; 

     @Override 
     protected void onResume() { 
      super.onResume(); 

      registerReceiver(mReceiver, mIntentFilter); 

     } 

     @Override 
     protected void onPause() { 

      super.onPause(); 

     } 

     @Override 
     protected void onDestroy() { 

      super.onDestroy(); 
     } 



    } 

これは、これは私が何度も試してみる私のgetData.class

で放送

Intent broadcastIntent = new Intent(); 
       broadcastIntent.setAction(MainPlayer.mBroadcastStringAction); 
       broadcastIntent.putExtra("duration", mediaPlayer.getDuration()); 
       broadcastIntent.putExtra("position", mediaPlayer.getCurrentPosition()); 
       sendBroadcast(broadcastIntent); 

を送信するために私のNotification.classです。しかし、それは動作していません。

答えて

1

onCreate

mIntentFilter = new IntentFilter(); 
mIntentFilter.addAction(mBroadcastStringAction); 
mIntentFilter.addAction(MUSIC_PLAYER); 

以下にインテントフィルタに MUSIC_PLAYERを追加
関連する問題