AlarmManager1 Android ã§Cronã¿ãããªãã¨ãããã«ã¯
Androidã§å¸¸é§ããã»ã¹(deamon)ã¿ãããªäºãããã«ã¯、Serviceãå©ç¨ãã¾ãã、æå®ããæéã«ã¢ããªã±ã¼ã·ã§ã³ãèµ·åãããæã¯、AlarmManagerã使ç¨ãã¾ã。
AlarmManager API ããã¥ã¡ã³ã
主ãªã¡ã½ããæ°ã¯ä»¥ä¸ã®ï¼ç¨®é¡ã¨ãªãã¾ã。
- æå®ããæéã«æå®ããPendingIntentãèµ·å
- æå®ããééã§æå®ããPendingIntentãèµ·å
- æå®ãPendingIntentããã£ã³ã»ã«ãã。
èµ·åããç©ã¨ãã¦ã¯、ãµã¼ãã¹ãããã¼ããã£ã¹ãã¬ã·ã¼ãã¼ãåå¨ãã¾ã。
ã¾ã、AlarmManagerã使ãä¸ã§ã®æ³¨æäºé
ãããã¾ãã®ã§é ã«è¨è¼ãã¦ããããã¨æãã¾ã。
æå®ããæéã«ããã¼ããã£ã¹ãã¬ã·ã¼ãã§åãåãããã°ã©ã
Broadcastéãå´
Intent intent = new Intent(ctx, ReceiverIconChange.class);intent.setAction(CALENDER_ICON_CHANGE);PendingIntent sender = PendingIntent.getBroadcast(ctx, 0, intent, 0); AlarmManager am = (AlarmManager)(ctx.getSystemService(ALARM_SERVICE)); // ç¿æ¥00:00:01ã®æéãè¨å®Calendar cal = Calendar.getInstance();cal.setTimeInMillis(currentTime);cal.add(Calendar.DATE, 1);cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 1);am.set(AlarmManager.RTC_WAKEUP,aTimerInMillis,sender);
ä¸è¨ãµã³ãã«ã§ã¯、ç¿æ¥ã®0æ00å01ç§ã«ã«ReceiverIconChange#onReviceã¡ã½ãããå¼ã°ãã¾ã。
ãã©ã¡ã¼ã¿ã¯、å¿
é ã§ã¯ããã¾ããã、ãã©ã¡ã¼ã¿ãå¿
è¦ãªå ´åã¯、setActionã、putExtraçã§é常ã®Intentã¨åãããã«æå®å¯è½ã§ã。
Broadcaståãåãå´
public class ReceiverIconChange extends BroadcastReceiver { public void onReceive(Context ctx, Intent intent) {String action = intent.getAction();if (!CALENDER_ICON_CHANGE.equals(action)) {// ã¢ã¤ã³ã³å¤æ´ã®è¦æ±ã§ãªãã£ãã、çµäºreturn;} doSomething();// 次ã®ã®Alarmãè¨å® // ç¿æ¥00:00:01ã®æéãè¨å®Calendar cal = Calendar.getInstance();cal.setTimeInMillis(currentTime);cal.add(Calendar.DATE, 1);cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 1);am.set(AlarmManager.RTC_WAKEUP,aTimerInMillis,sender); }
setRepeatã§ã¯、ããªç§æ¯ã«èµ·åããBroadcastReceiverãæå®ã§ãã¾ãã、setã§è¨å®ã§ããã®ã¯、ï¼åã®èµ·åã®ã¿ã§ã。å¾ã£ã¦ä¸è¨ã®ãµã³ãã«ã³ã¼ãã§ã¯、BroadcastReceiverå
ã§å度、AmarmManagerã使ç¨ãã¦æ¬¡åã®èµ·åç»é²ãè¡ã£ã¦ããã¾ã。
doSomethingã¡ã½ããé¨åã§å®éã«ãã¸ãã¹ãã¸ãã¯ãè¨è¿°ããããã§ãã、BroadcastReceiverã§ã¯、ãã¾ãéããå¦çããã¹ãã§ã¯ããã¾ãã。æéã®ãããå¦ç(ãã¡ã¤ã«ã®ãã¦ã³ãã¼ãç)ã、UIã表示ãããããªå ´åã¯、BroadcastReceiverã§åãåãã®ã§ã¯ãªã、Serviceãå®è£
ãã¦åãåãã¾ã。
Manufestãã¡ã¤ã«
ããã¼ããã£ã¹ãã¬ã·ã¼ãã¼ã使ç¨ããå ´åã¯、Manufestãã¡ã¤ã«ã¸ã®è¨è¼ãå¿ è¦ã¨ãªãã¾ã。
<receiver android:name=".ReceiverIconChange" ><intent-filter><action android:name="jp.co.taosoftware.android.calendar.action.ICON_CHNAGE" /></intent-filter></receiver>
é常ã«ç°¡åãªãµã³ãã«ã§ãã、ããã§cronã®ãããªåä½ãããããã¨ãå¯è½ã§ã。
注æäºé
ã¨ãã¦ã¯、ããã¥ã¡ã³ãã«æ¸ãã¦ããã¾ãã、setãï¼åé£ç¶ãã¦å¼ãã§ã¯ããã¾ãã。(ã¹ã¬ããåãã¦ããæã¯ç¹ã«æ³¨æ)ããªãã、cancelããã¦ããåè¨å®ãã¦ãã ãã。
å®éï¼åé£ç¶ã§setãèªãã§è¿½ã£ã¦ã¿ã¾ããã、ã©ããããããªãåãããã¾ãã。ãã®ãããã®åä½ã¯ä»å¾ã®ï¼¯ï¼³ã®ãã¼ã¸ã§ã³ã¢ããã§å¤æ´ã«ãªãå¯è½æ§ãããã¾ãã®ã§、ç§ã®ããã«ãã¾ãæ·±å
¥ãããªãã®ãè¯ãã§ããã。
ãµã¼ãã¹ã§ã®åãåãæ¹ã、ãã®ä»æ³¨æäºé
ã«é¢ãã¦ã¯æ¬¡åã«…
ããã°å ã®é¢é£ããè¨äº
- AlarmManager 1 Android ã§Cronã¿ãããªãã¨ãããã«ã¯
- AlarmManager 2 å®æçãªæè¦ã§ç¹°ãè¿ãå¦çãããããã°ã©ã
- Android AlarmManager 3 ãµã¼ãã¹ã§ã®åãåã
- Android AlarmManager 4 ã¢ã©ã¼ã ãæ¶ããã¨ã
- Android AlarmManager 5 ã·ã¹ãã ããã¼ããã£ã¹ã
ã³ã¡ã³ã
ã³ã¡ã³ããæ稿