0

이 방법으로 처리하려고합니다 : BroadcastReceiver 시작 AlarmManager 반복 작업에서 의도를 IntentService으로 보내면 서비스 로그가 기록됩니다. 로그에서 알 수 있듯이 BroadcastReceiver은 수신 의도가 있으며 AlarmManager을 시작하지만 IntentService은 실행되지 않습니다. 여기서 무엇이 잘못 될 수 있습니까?AlarmManager를 사용하여 IntentService를 고칠 수 없습니다.

매니페스트 :

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

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

WakefulReciever :

public class SimpleWakefulReciever extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (!App.isRunning) { 
      Log.d("wakefull", "start"); 
      Intent startIntent = new Intent(context, NotificationWakefulIntentService.class); 
      startIntent.setAction(Utils.NOTIFY_INTENT); 
      PendingIntent startPIntent = PendingIntent.getBroadcast(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; 
     } 
    } 
} 

IntentService :

public class NotificationWakefulIntentService extends IntentService { 

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

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

답변

0

당신은 명시 적으로 ServiceIntent를 정의하지만, getBroadcast() 대신 getService()를 호출하고 있습니다.

은 다음과 변경

:

PendingIntent startPIntent = PendingIntent 
    .getBroadcast(context, 0, startIntent, 0); 

이 사람 :

PendingIntent startPIntent = PendingIntent 
    .getService(context, 0, startIntent, 0); 

또한,이 방법 WakefulBroadcastReceiver 작동하지 않습니다. 도우미 클래스이며 그 목적은 Service이 작업을 마칠 때까지 WakeLock을 제공하는 것입니다.

단순히 WakefulBroadcastReceiver을 연장하면 아무런 성과가 없습니다. 은 onReceive() 동안 보장됩니다. 당신은 (this answer 체크 아웃) 매 시간마다 화재 WakefulBroadcastReceiver.startWakefulService()를 호출하여 onReceive()에서 IntentService를 시작 onHandleIntent()에 물건을 마치면 WakefulBroadcastReceiver.completeWakefulIntent()를 호출하는 정확한 알람을 설정해야

:

아래에 귀하의 코멘트에 대답합니다.

관련 문제