2017-04-04 5 views
2

현재 Android Alarm Manager로 작업하고 있으며 작동 예제를 발견했습니다. 하지만 내 상황에서는 제대로 작동하지 않습니다. 설명하겠습니다. 기본적으로 내 목표는 매 5 분마다 MainActivity에서 메소드를 실행하는 것입니다. 이를 위해 Alarm Manager를 사용하여 해당 작업을 예약합니다.작업 예약시 알람 관리자가 예상대로 작동하지 않습니다.

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     context.sendBroadcast(new Intent("SERVICE_TEMPORARY_STOPPED")); 
    } 
} 

MainActivity.java

public class MainActivity extends Activity{ 
    private PendingIntent pendingIntent; 
    private AlarmManager manager; 

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); 
     } 
    }; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
     registerReceiver(broadcastReceiver, new IntentFilter("SERVICE_TEMPORARY_STOPPED")); 

     Button button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startAlarm(); 
      } 
     }); 
    } 


    public void startAlarm() { 
     manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     int interval = 300000; 
     manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); 
     Log.d(TAG, "Alarm Set"); 
    } 
} 

모든 것이 좋다 :

은 기본적으로이 작업 물건입니다. "실행 중"입니다. 토스트는 매 300,000ms (5 분)마다 실행됩니다. AlarmReceiver 클래스는 "SERVICE_TEMPORARY_STOPPED"메시지와 함께 내 주요 활동에 브로드 캐스트를 보냅니다. 나는 이미 메시지를 내 MainActivity에 registerReceiver(broadcastReceiver, new IntentFilter("SERVICE_TEMPORARY_STOPPED"));을 통해 등록했습니다. 그러나 다른 방법을 추가 할 때 broadcastReceiver에 stopAlarm()이라고 가정 해 봅니다. 그러면 5 분이 지나면 알람이 중지되고 더 이상 시간 간격 (5 분)이 적용되지 않습니다. 10 초 정도의 시간이 지나면 브로드 캐스트 수신기를 호출하고 알람을 중지합니다. 그리고 이것이 문제입니다. stop() 방법을 살펴보고 난은 broadcastReceiver에 전화하는 방법 :

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); 
     stopAlarm(); 
    } 
}; 

public void stopAlarm() { 
    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    manager.cancel(pendingIntent); 
    Log.d(TAG, "Alarm Cancelled"); 
} 

어떤 단서?

답변

0

AlarmManager.setRepeating은 다양한 Android 버전에서 제대로 작동하지 않습니다.

시도 setExact. 그것은 반복하지 않습니다하지만 당신은 아래에 언급 한 바와 같이 기능을 반복 얻을 수 있습니다

여기

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     context.sendBroadcast(new Intent("SERVICE_TEMPORARY_STOPPED")); 

     long repeatCount = PreferenceManager.getDefaultSharedPreferences(context).getLong("REPEAT_COUNT", 0L); 

     repeatCount++; 

     PreferenceManager.getDefaultSharedPreferences (context).edit().putLong("REPEAT_COUNT", repeatCount).apply() 

     AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
     manager.setExact(AlarmManager.RTC_WAKEUP, (repeatCount *System.currentTimeMillis()),pendingIntent); 
    } 
} 

우리가 & 변수 (환경 설정 기준) 반복 횟수를 유지 AlarmReceiver.java 업데이트하고 AlarmReceiver &에를 증가 repeatCount * System.currentTimeMillis();를 사용하여 nextAlarmTime을 계산하여 경보를 다시 예약하십시오.

관련 문제