Android Alarm目覚まし時計

2243 ワード

1、目覚まし時計をセットする(一回だけ鳴らす)

Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 30);

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

2、目覚まし時計を繰り返す
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
            
            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 15*1000;

            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 15*1000, sender);

3、目覚まし時計のキャンセル
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
            
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(sender);

---------------------------------------------分割線-----------------------------------------------------
一:OneShotAlarm.class/RepeatingAlarm.class
その他のActivity(Activity,Service,BroadcastReceiver)
二:PendingIntent字面はストラップの意図を意味する
日语解释:*A description of an Intent and target action to perform with it. 
* Instances of this class are created with {@link #getActivity},
* {@link #getBroadcast}, {@link #getService}; the returned object can be
* handed to other applications so that they can perform the action you
* described on your behalf at a later time.
PendingIntentはIntentの説明で、私たちはこの説明を他のプログラムに渡すことができます.他のプログラムはこの説明に基づいて後の別の時間にあなたが手配したことをします.