2015-04-01 73 views
0

我有音樂服務播放音頻文件。我從我的活動開始這項服務。我發送播放/暫停按鈕時,通知我發送廣播接收器播放按鈕時,它的工作。在onReceive(Context context,Intent intent)方法中,我調用了context.startService(intentService),問題是MusicService.onStartCommand()從未調用過。從BroadcastReceiver啓動服務從不調用service.onStartCommand()方法

我的接收機

public class PlayEpisodeReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Service Started", Toast.LENGTH_SHORT).show(); 
     if(MusicService.isRunning()) { 

      Intent serviceIntent = new Intent(context, MusicService.class); 

      serviceIntent.putExtra(AppConstants.ACTION_TYPE, AppConstants.ACTION_PLAY); 

      context.startService(intent); 
     } 
    } 

} 

我的清單:

<service 
      android:enabled="true" 
      android:name=".backgroundtasks.MusicService"> 
      <intent-filter> 
       <action 
        android:name = "cc.gm.oscarradio.backgroundtasks.MusicService"> 
       </action> 
      </intent-filter> 
     </service> 
     <receiver 
      android:enabled="true" 
      android:name=".Receivers.PlayEpisodeReceiver"> 
      <intent-filter> 
       <action android:name = "cc.gm.oscarradio.ACTION_MUSIC"/> 
      </intent-filter> 
     </receiver> 

回答

2

在你的代碼應該有一個細微的變化,我覺得,你應該替換下面的行:由

context.startService(intent); 

context.startService(serviceIntent); 

希望這可以幫助你。

+0

謝謝你現在工作。 – 2015-04-01 13:18:38

+0

很酷。感謝接受。快樂的編碼。 – Keshav1234 2015-04-01 13:19:54