天天看點

Android---AlarmManager(全局定時器/鬧鐘)指定時長或以周期形式執行某項操作

AlarmManager的使用機制有的稱呼為全局定時器,有的稱呼為鬧鐘。通過對它的使用,個人覺得叫全局定時器比較合适,其實它的作用和Timer有點相似。都有兩種相似的用法:(1)在指定時長後執行某項操作(2)周期性的執行某項操作

AlarmManager對象配合Intent使用,可以定時的開啟一個Activity,發送一個BroadCast,或者開啟一個Service.

下面的代碼詳細的介紹了兩種定時方式的使用:

 (1)在指定時長後執行某項操作

代碼

//操作:發送一個廣播,廣播接收後Toast提示定時操作完成   
     Intent intent     =
    new
     Intent(Main.
    this
    , alarmreceiver.
    class
    );
    intent.setAction(    "
    short
    "
    );
    PendingIntent sender    =
    
        PendingIntent.getBroadcast(Main.    this
    , 
    0
    , intent, 
    0
    );


    //設
    定一個五秒後的時間
    

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

    AlarmManager alarm    =
    (AlarmManager)getSystemService(ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

    //
    或者以下面方式簡化

        //
    alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
    

        
    Toast.makeText(Main.    this
    , 
    "
    五秒後alarm開啟
    "
    , Toast.LENGTH_LONG).show();  ​
    //注意:receiver記得在manifest.xml注冊      

​​

​​

代碼

public         static
     
    class
     alarmreceiver 
    extends
     BroadcastReceiver{

        @Override

    public
     
    void
     onReceive(Context context, Intent intent) {

    //
     TODO Auto-generated method stub
    

                
    if
    (intent.getAction().equals(
    "
    short
    "
    )){
                Toast.makeText(context,     "    short alarm    "
    , Toast.LENGTH_LONG).show();
            }    else    {
                Toast.makeText(context,     "    repeating alarm    "
    ,    
                      Toast.LENGTH_LONG).show();
            }
        }
    }  ​      

​​

(2)周期性的執行某項操作

​​

代碼

Intent intent     =    new     Intent(Main.    this    , alarmreceiver.
    class
    );
    intent.setAction(    "    repeating    "    );
    PendingIntent sender    =    PendingIntent
        .getBroadcast(Main.    this    ,     0    , intent,     0
    );   
    //開始時間

    long
     firstime
    =
    SystemClock.elapsedRealtime();

    

        AlarmManager am
    =
    (AlarmManager)getSystemService(ALARM_SERVICE);   
  //5秒一個周期,不停的發送廣播
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
            , firstime,     5    *    1000    , sender);  ​      

​​

 AlarmManager的setRepeating()相當于Timer的Schedule(task,delay,peroid);有點差異的地方時Timer這個方法是指定延遲多長時間

以後開始周期性的執行task;

AlarmManager的取消:(其中需要注意的是取消的Intent必須與啟動Intent保持絕對一緻才能支援取消AlarmManager)

​​

代碼

Intent intent     =    new     Intent(Main.    this    , alarmreceiver.    class    );
  intent.setAction(    "    repeating    "    );
  PendingIntent sender    =    PendingIntent
         .getBroadcast(Main.    this    ,     0    , intent,     0    );
  AlarmManager alarm    =    (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);  ​