0

次のスキーマBOOT_COMPLETEインテント - >BroadcastRecieverが開始されてから、NotificationIntentServiceに送信するWakefullBroadcastReceiverを開始します。WakefulBroadcastReceiverは呼び出されません

しかし、私は意図BOOT_COMPLETEにスキーマを変更した場合、私のWakefullBroadcastReceiverは、作業されることはありません - >BroadcastReciever - >NotificationIntentServiceに意図を送信します。

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> 
012 everethingは完全に

BroadcastReceiverの作品

答えて

0

まず、<intent-filter>の要素をすべて取り除き、<action android:name="android.intent.action.BOOT_COMPLETED"/>の要素を取り除きます。任意のサードパーティ製のアプリケーションでいつでもコンポーネントを起動できるようにしない限り、コンポーネントに<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を修正しないと、あなたがトリガーしようとしていることから、getBroadcast()を使用するようにPendingIntentを修正BroadcastReceiver

次に、Android 5.1以降のアラームより頻繁にアラームを繰り返すことはできないため、5000を値60000以上に置き換えてください。

+0

ありがとうございました!今、それは動作します、私は、アンドロイドバージョン15のデバイスで目的のために、ちょうどアラームのための1分を待つのを避けるためにテストしています。 – Turbozanik

関連する問題