2012-08-24 42 views
1

大家好,感謝您的幫助! 我會用代碼開始
所以,用廣播接收器類是這樣的:Android BroadcastReceiver從未被稱爲

public class MyService extends Service { 

    // ... 
    // ACTION 
    public static final String action = "com.mywebsite.myapp.package.class.action"; 
    // ... 
    public void onCreate() { 
     // SET BROADCAST RECEIVER 
     broadcastReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       Log.w("broadcast receiver", "action: " + action); 
       if (action.equals("**")) { 
        Log.w("broadcast receiver", "**"); 
       } 
      } 
     }; 
     // REGISTER BROADCAST 
     final IntentFilter myFilter = new IntentFilter(); 
     myFilter.addAction(action); 
     registerReceiver(this.broadcastReceiver, myFilter); 
    } 
    // .... 
} 

我嘗試以這種方式從一個片段

Intent myIntent = new Intent(getActivity() 
     .getApplicationContext(), MediaPlayerService.class); 
getActivity().startService(myIntent); 
myIntent = new Intent(getActivity().getApplicationContext(), 
     MediaPlayerService.class); 
myIntent.setAction(MyService.action); 
myIntent.putExtra("data", "*******"); 
getActivity().sendBroadcast(myIntent); 

然而,發送一個廣播廣播接收機從不被調用。我可以這樣說,因爲logcat:Log.w(「broadcast receiver」,「action:」+ action);永遠不會被調用。我該如何解決?

謝謝!

編輯:類代碼:

public class MediaPlayerService extends Service { 

    private MediaPlayer mediaPlayer = null; 
    private AudioManager audioManager; 
    private BroadcastReceiver broadcastReceiver; 
    private String absoluteFilePath; 
    private Boolean areThereAnyErrors = false; 
    private int savedVolume; 
    // ACTIONS 
    public static final String prepareAndPlayNewFile = "com.disgustingapps.player.AudioManagement.MediaPlayerService.prepareAndPlayNewFile"; 

    @Override 
    public void onCreate() { 
     Log.w("MediaPlayerService", "onCreate called"); 
     // SET BROADCAST RECEIVER 
     broadcastReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       Log.w("broadcast receiver", "action: " + action); 
       if (action.equals("prepareAndPlayNewFile")) { 
        Log.w("broadcast receiver", "prepareAndPlayNewFile"); 
        prepareAndPlayNewFile(intent 
          .getStringExtra("absoluteFilePath")); 
       } 
      } 
     }; 
     // REGISTER BROADCAST 
     final IntentFilter myFilter = new IntentFilter(); 
     myFilter.addAction(prepareAndPlayNewFile); 
     registerReceiver(this.broadcastReceiver, myFilter); 
    } 

    @Override 
    public void onDestroy() { 
     Log.w("MediaPlayerService", "onDestroy called"); 
     unregisterReceiver(broadcastReceiver); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.w("MediaPlayerService", "onStartCommand called"); 
     // We want this service to continue running until it is explicitly 
     // stopped, so return sticky. 
     return START_STICKY; 
    } 

    public void prepareAndPlayNewFile(String absoluteFilePath) { 
     // ...do something... 
    } 
} 
+1

當您嘗試發送廣播時,是否確認服務正在運行?我猜想在發送廣播之前,該服務尚未註冊BroadcastReceiver。 – Bobbake4

+0

同意@ Bobbake4。向你的'Service'添加一些額外的日誌記錄,以顯示它註冊接收器並將一些添加到'Fragment'以顯示它發送廣播。 – Squonk

+0

我檢查了兩個,他們跑!那麼問題是什麼? – user1315621

回答

0

您沒有收到Intent的原因是因爲接收器在onCreate()context.startService()只會通過ActivityIntentServiceonStartCommand()Service已經創建(或應該有),所以試圖在onCreate()接收將永遠不會工作。

更多信息here

試圖接收onStartCommand()只能使用一次(猜)。但如果您撥打startService(),在onStartCommand中輸入BroadcastReceiver只會爲您提供數據。既然你是這樣稱呼它的話,那麼只要把BroadcastReceiver丟掉,然後把Intent發送到ServiceonStartCommand()就可以了。

編輯

@Override 
public void onCreate() { 
    Log.w("MediaPlayerService", "onCreate called"); 
} 

@Override 
public void onDestroy() { 
    Log.w("MediaPlayerService", "onDestroy called"); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.w("MediaPlayerService", "onStartCommand called");  

    String action = intent.getAction(); 
    Log.w("onStartCommand()", "action: " + action); 

    if (action.equals("prepareAndPlayNewFile")) { 
     prepareAndPlayNewFile(intent.getStringExtra("absoluteFilePath")); 
    } 

    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    return START_STICKY; 
} 

然後只是這樣做:

​​

startService()

這種方法每次調用會導致對目標服務的onStartCommand相應通話(Intent,int,int)方法,其意圖是h ERE。這提供了一種便捷的方式將作業提交給服務,而無需綁定並調用其接口。

+0

我試過但是不行。也許是因爲我需要在服務啓動後接收廣播,而服務正在運行。可能是這個原因嗎?謝謝 – user1315621

+0

但是在這點爲什麼要用廣播? – user1315621

+0

啊我明白你的意思了。等等,我會嘗試一些東西 – slinden77

1

在沒有看到代碼服務,以及如何開始它的很難說哪裏的問題..

首先,你要註冊的接收器服務的onCreate,但你沒有註銷事件..你應該把廣播接收器放在你的服務類的全局範圍內,並在onDestroy()中取消註冊(重寫onDestroy並記錄消息以確認其停止)

第二,除非你綁定服務,然後服務將在啓動命令執行後立即「停止」。

您應該重寫onStartCommand並返回START_STICKY以保持服務正在運行。

+0

它返回START_STICKY! 我編輯我的第一篇文章與問題類的部分的代碼。希望你能幫我!並且預先感謝你很多:) – user1315621