0

我有以下架構BOOT_COMPLETE意圖 - >BroadcastReciever開始,然後開始WakefullBroadcastReceiver - >發送意向NotificationIntentServiceWakefulBroadcastReceiver永遠不會被調用

但我WakefullBroadcastReceiver是從來沒有工作,如果我改變模式以BOOT_COMPLETE意圖 - >BroadcastReciever - >發送意圖NotificationIntentService。 everething完美的作品

廣播接收器:

public class BootBroadcastReciever extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (!App.isRunning) { 
       Log.d("wakefull", "start"); 
       Intent startIntent = new Intent("SOMEACTION"); 
       //startIntent.setAction(Utils.NOTIFY_INTENT); 
       PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0); 
       AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
       am.setRepeating(AlarmManager.RTC_WAKEUP, 
         SystemClock.elapsedRealtime() + 3000, 5000, startPIntent); 
       App.isRunning = true; 
      } 
      Log.e("bool",App.isRunning+""); 
     } 
    } 

WakefulBroadcastReceiver:

public class SimpleWakefulReciever extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.e("wakefull","received"); 
     Intent service = new Intent(context, NotificationWakefulIntentService.class); 
     startWakefulService(context,service); 
    } 
} 

NotificationIntentService:

public class NotificationWakefulIntentService extends IntentService { 

    public NotificationWakefulIntentService() { 
     super("NotificationWakefulIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d("time",(System.currentTimeMillis()/1000)+""); 
     SimpleWakefulReciever.completeWakefulIntent(intent); 
    } 
} 

清單:

<receiver android:name=".broadCastReciever.BootBroadcastReciever" android:enabled="true" android:exported="false"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       <action android:name="START"/> 
      </intent-filter> 
     </receiver> 

     <receiver android:name=".wakefullBroadcastReciever.SimpleWakefulReciever"> 
      <intent-filter> 
       <action android:name="SOMEACTION"/> 
       <action android:name="WAKEFUL"/> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name=".wakefulService.NotificationWakefulIntentService" 
      android:enabled="true"> 
      <intent-filter> 
       <action android:name="NOTIFY_INTENT" /> 
      </intent-filter> 
     </service> 

回答

0

首先,擺脫除了一個包裝<action android:name="android.intent.action.BOOT_COMPLETED"/>以外的所有<intent-filter>元素。永遠不要在組件上放置<intent-filter>,除非您希望任意第三方應用程序隨時啓動該組件。

然後,替換:

Intent startIntent = new Intent("SOMEACTION"); 
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0); 

有:

Intent startIntent = new Intent(context, SimpleWakefulReciever.class); 
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0); 

這解決您的Intent不再使用操作字符串和修復您的PendingIntent使用getBroadcast(),因爲你正試圖觸發BroadcastReceiver

然後,將5000替換爲至少60000的值,因爲您不會比Android 5.1+更頻繁地發出重複鬧鐘。

+0

非常感謝!現在,它的工作原理,我正在測試手機上的Android版本15,只是爲了避免等待一分鐘的警報。 – Turbozanik

相關問題