AlarmManager是Android提供的一個(gè)全局定時(shí)器,利用系統(tǒng)鬧鐘定時(shí)發(fā)送廣播。這樣做的好處是:如果App提前注冊(cè)鬧鐘的廣播接收器,即使App退出了,只要定時(shí)到達(dá),App就會(huì)被喚醒響應(yīng)廣播事件。
AlarmManager設(shè)置的PendingIntent待定意圖,只要未cancel,就會(huì)按時(shí)啟動(dòng),無(wú)論程序是否關(guān)閉。
請(qǐng)注意,靜態(tài)注冊(cè)的廣播接收者在即使程序關(guān)閉也依然生效;動(dòng)態(tài)注冊(cè)的廣播接收者在程序關(guān)閉后自動(dòng)注銷(xiāo),動(dòng)態(tài)注冊(cè)的廣播接收者也可使用unregisterReceiver()手動(dòng)注銷(xiāo)。在Android8.0以后,只有小部分接收系統(tǒng)廣播的廣播接收者允許靜態(tài)注冊(cè);其他廣播接收者只能動(dòng)態(tài)注冊(cè),否則收不到廣播。
一、使用方法
(1) 創(chuàng)建用于廣播的Intent(意圖)
Intent intent=new Intent();
intent.setAction( "MyTestBroadcast" );
(2) 創(chuàng)建發(fā)送廣播的PendingIntent(待定意圖)
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,2333,intent,PendingIntent.FLAG_IMMUTABLE);
第一個(gè)參數(shù)為環(huán)境;第二個(gè)參數(shù)為請(qǐng)求碼;第三個(gè)參數(shù)為要執(zhí)行的Intent(意圖);第四個(gè)參數(shù)為請(qǐng)求時(shí)PendingIntent已存在的解決方案。?
(3) 創(chuàng)建執(zhí)行PendingIntent的時(shí)間
//創(chuàng)建Calendar
Calendar calendar=Calendar.getInstance();
//將時(shí)間設(shè)置為當(dāng)前時(shí)間
calendar.setTimeInMillis(System.currentTimeMillis());
//增加時(shí)間
calendar.add(Calendar.SECOND,7);
//獲取最終時(shí)間
long time=calendar.getTimeInMillis();
(4) 創(chuàng)建AlarmManager并設(shè)置PendingIntent
//創(chuàng)建AlarmManager鬧鐘管理者
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
//設(shè)置鬧鐘
alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent);
alarmManager.set()方法的參數(shù):第一個(gè)參數(shù)為鬧鐘類(lèi)型,第二個(gè)參數(shù)為long型的執(zhí)行PendingIntent的時(shí)間,第三個(gè)參數(shù)為到達(dá)時(shí)間后執(zhí)行的PendingIntent。
二、鬧鐘類(lèi)型
(1) ELAPSED_REALTIME :?以手機(jī)開(kāi)機(jī)時(shí)間為基準(zhǔn)。
(2) ELAPSED_REALTIME_WAKEUP : 以手機(jī)開(kāi)機(jī)時(shí)間為基準(zhǔn),并且可以在休眠時(shí)發(fā)出廣播。
(3) RTC : 以UTC標(biāo)準(zhǔn)時(shí)間為基準(zhǔn)。
(4) RTC_WAKEUP 【常用】:以 UTC標(biāo)準(zhǔn)時(shí)間為基準(zhǔn),并且可以在休眠時(shí)發(fā)出廣播。
三、使用AlarmManager
//創(chuàng)建Intent意圖,用于發(fā)送廣播
Intent intent=new Intent().setAction("MyTestBroadcast");
//根據(jù)Intent意圖創(chuàng)建PendingIntent等待意圖
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,2333,intent,PendingIntent.FLAG_IMMUTABLE);
//獲取執(zhí)行時(shí)間
//創(chuàng)建Calendar
Calendar calendar=Calendar.getInstance();
//將時(shí)間設(shè)置為當(dāng)前時(shí)間
calendar.setTimeInMillis(System.currentTimeMillis());
//增加時(shí)間
calendar.add(Calendar.SECOND,7);
//獲取最終時(shí)間
long time=calendar.getTimeInMillis();
//創(chuàng)建AlarmManager鬧鐘管理者
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
//設(shè)置鬧鐘
alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent);
四、例子
1.創(chuàng)建廣播接收者
AndroidManifest.xml清單文件
<application
......>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true"></receiver>
<activity
......>
......
</activity>
</application>
MyBroadcastReceiver.java文件文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-740674.html
public class MyBroadcastReceiver extends BroadcastReceiver{
private Context context;
public MyBroadcastReceiver(Context context){
this.context=context;
}
public void onReceive(Context context, Intent intent) {
//廣播接收者
NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel=new NotificationChannel("id","name",NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Notification.Builder builder=new Notification.Builder(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("id");
}
builder.setContentText("接收到廣播");
builder.setSmallIcon(R.drawable.icon2);
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
Notification notification=builder.build();
notificationManager.notify(2333,notification);
}
}
2.注冊(cè)廣播接收者&&設(shè)置定時(shí)發(fā)送廣播鬧鐘
MainActivity.java文件文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-740674.html
public class MainActivity extends AppCompatActivity {
private MyBroadcastReceiver myBroadcastReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注冊(cè)廣播接收者-接收"MyTestBroadcast"廣播
myBroadcastReceiver=new MyBroadcastReceiver(MainActivity.this);
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("MyTestBroadcast");
registerReceiver(myBroadcastReceiver,intentFilter);
//獲取控件
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//創(chuàng)建Intent意圖,用于發(fā)送廣播
Intent intent=new Intent().setAction("MyTestBroadcast");
//根據(jù)Intent意圖創(chuàng)建PendingIntent等待意圖
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,2333,intent,PendingIntent.FLAG_IMMUTABLE);
//獲取執(zhí)行時(shí)間
//創(chuàng)建Calendar
Calendar calendar=Calendar.getInstance();
//將時(shí)間設(shè)置為當(dāng)前時(shí)間
calendar.setTimeInMillis(System.currentTimeMillis());
//增加時(shí)間
calendar.add(Calendar.SECOND,7);
//獲取最終時(shí)間
long time=calendar.getTimeInMillis();
//創(chuàng)建AlarmManager鬧鐘管理者
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
//設(shè)置鬧鐘
alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent);
//銷(xiāo)毀頁(yè)面
MainActivity.this.finish();
}
});
}
protected void onDestroy() {
super.onDestroy();
// 不 注銷(xiāo)廣播接收者
if(myBroadcastReceiver!=null){
//unregisterReceiver(myBroadcastReceiver);
}
}
}
到了這里,關(guān)于AlarmManager鬧鐘管理者的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!