AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其
实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作。
AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.
下面的代码详细的介绍了两种定时方式的使用:
(1)在指定时长后执行某项操作:
MainAct.java:
public class MainAct extends Activity { Toast mToast; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("oneshot"); PendingIntent pintent = PendingIntent.getBroadcast(MainAct.this, 0, intent, 0); //根据传入的NAME来取得对应的Object,然后转换成相应的服务对象 AlarmManager am = (AlarmManager)MainAct.this.getSystemService(ALARM_SERVICE); Calendar canlender = Calendar.getInstance();//获取日历的实例 canlender.setTimeInMillis(System.currentTimeMillis());// 代表当前时间的日历 canlender.add(canlender.SECOND, 30);// 将时间设置为现在的30s以后 (定时时间) //设置并启动定时器 am.set(AlarmManager.RTC_WAKEUP, canlender.getTimeInMillis(), pintent); if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(MainAct.this, "one shot alarm will go off in 30 seconds.", Toast.LENGTH_LONG); //弹出Toast,用于提示定时任务已经开启 mToast.show(); } }); } }
清单文件:
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainAct" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".OneShotAlarm"> <intent-filter> <action android:name="oneshot"/> </intent-filter> </receiver> </application>
OneShotAlarm.java:
public class OneShotAlarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show(); } }
(2)周期性的执行某项操作
start repeating AlarmManager:
Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("repeating"); PendingIntent sender = PendingIntent.getBroadcast(MainAct.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. //系统启动到目前为止的时间 long firstTime = SystemClock.elapsedRealtime(); firstTime += 15*1000; // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation) //以15秒一个周期发送广播... am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(MainAct.this, "已启动周期性计时器~", Toast.LENGTH_LONG); mToast.show(); } });
stop repeating Alarm:
Button btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("repeating"); PendingIntent sender = PendingIntent.getBroadcast(MainAct.this, 0, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(MainAct.this, "alarm canceled", Toast.LENGTH_LONG); mToast.show(); } });