android2019. 11. 27. 14:29

 

주의 사항: 

 

AndroidManifest에 아래와 같이 receiver 속성을 지정한다.

android:process=":remote" 없는 경우 동작하지 않음.

 

android:process=":remote"

android:name=".Alarm"


정확도와 overhead를 고려 어느 set함수를 호출할지 선택한다.

  setRepeating vs setInexactRepeating 

차이점은 검색해 보도록, 알람의 주기는 최소 1분이 되어야함.


public class Alarm extends BroadcastReceiver {
    private final String LOG_TAG = "WifiMonitoringService";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d(LOG_TAG,"Alarm");
        Toast.makeText(context, "Alarm", Toast.LENGTH_LONG ).show();

    }
    public void setAlarm(Context context)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

        //am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, 1000 * 60 * 1, pi); // Millisec * Second * Minute
        am.setInexactRepeating(AlarmManager.RTC,
                System.currentTimeMillis() + 1000, 60*1000, pi);
    }

    public void cancelAlarm(Context context)
    {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}



 

 

참조 : https://developer.android.com/training/scheduling/alarms
https://androidclarified.com/android-example-alarm-manager-complete-working/

'android' 카테고리의 다른 글

PDK에서 Android studio app을 gradle build 시 tips  (0) 2019.11.27
android internet 연결 확인  (0) 2019.11.27
Android bp usage  (0) 2019.10.15
voice search later Android P  (0) 2019.09.26
Simple requestPermission  (0) 2019.09.26
Posted by easy16