Android通過(guò)AlarmManager類實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
Android通過(guò)AlarmManager類實(shí)現(xiàn)鬧鐘,供大家參考,具體內(nèi)容如下
簡(jiǎn)介
鬧鐘是生活中最常用的功能了,很多App都可以加入該功能,提醒用戶某個(gè)時(shí)刻要做的事情。在Android系統(tǒng)中可以通過(guò)AlarmManager類實(shí)現(xiàn)鬧鐘,AlarmManager類是專門用來(lái)設(shè)定在某個(gè)指定的時(shí)間去完成指定的事件。AlarmManager提供了訪問(wèn)系統(tǒng)警報(bào)的服務(wù),只要在程序中設(shè)置了警報(bào)服務(wù),AlarmManager就會(huì)通過(guò)onReceive()方法去還行這些事件,就算系統(tǒng)處于待機(jī)狀態(tài),同樣不會(huì)影響運(yùn)行??梢酝ㄟ^(guò)Context.getSystemService方法來(lái)獲取該服務(wù)。接下來(lái)我們將使用AlarmManager來(lái)制作一個(gè)最簡(jiǎn)單的鬧鐘。
讓我們來(lái)看一下AlarmManager都為我們提供了哪些方法,如下:

要實(shí)現(xiàn)鬧鐘,首先需要?jiǎng)?chuàng)建一個(gè)繼承自BroadcastReceiver的類,實(shí)現(xiàn)onReceive方法來(lái)接收這個(gè)Alarm服務(wù),然后通過(guò)建立Intent和PendingIntent連接來(lái)調(diào)用Alarm組件,并通過(guò)TimePickerDialog來(lái)設(shè)置時(shí)間,當(dāng)時(shí)間到我們指定的時(shí)間后onReceive方法接收到Alarm服務(wù)后即可進(jìn)行提示。
讓我們實(shí)現(xiàn)主界面布局,效果如下:

接下來(lái)讓我們實(shí)現(xiàn)接收Alarm服務(wù)的AlarmReceiver類,該類比較簡(jiǎn)單,在收到消息后用一個(gè)Toast來(lái)提示用戶,具體實(shí)現(xiàn)代碼如下:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "您設(shè)置的時(shí)間到了!",
Toast.LENGTH_SHORT).show();
}
}
由于使用了BroadcastReceiver,因此我們需要在AndroidManifest.xml文件中對(duì)其進(jìn)行聲明,如下:
<receiver android:name=".AlarmReceiver" android:process=":remote" />
接下來(lái),在MainActivity中我們實(shí)現(xiàn)“設(shè)置鬧鐘”和“取消鬧鐘”的事件監(jiān)聽,讓我們來(lái)看一下具體實(shí)現(xiàn)代碼:
public class MainActivity extends Activity {
private Button btnSet, btnCancel;
private TextView info;
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSet = (Button) findViewById(R.id.setalarm);
btnCancel = (Button) findViewById(R.id.cancelalarm);
info = (TextView) findViewById(R.id.info);
calendar = Calendar.getInstance();
btnSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calendar.setTimeInMillis(System.currentTimeMillis());
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMinute = calendar.get(Calendar.MINUTE);
new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
// TODO Auto-generated method stub
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// 建立Intent和PendingIntent來(lái)調(diào)用目標(biāo)組件
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// 獲取鬧鐘管理的實(shí)例
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// 設(shè)置鬧鐘
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
// 設(shè)置周期鬧鐘
am.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (10 * 1000),
(24 * 60 * 60 * 1000), pendingIntent);
String tmpS = "設(shè)置鬧鐘時(shí)間為" + format(hourOfDay)
+ ":" + format(minute);
info.setText(tmpS);
}
}, mHour, mMinute, true).show();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
// 獲取鬧鐘管理實(shí)例
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// 取消
am.cancel(pendingIntent);
info.setText("鬧鐘已經(jīng)取消");
}
});
}
// 格式化字符串7:3-->07:03
private String format(int x) {
String s = "" + x;
if (s.length() == 1) {
s = "0" + s;
}
return s;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
在上述代碼中我們使用了PendingIntent,PendingIntent這個(gè)類用于處理即將發(fā)生的事情,PendingIntent可以看作是對(duì)Intent的包裝,通常通過(guò)getActivity、getBroadcast、getService來(lái)得到PendingIntent的實(shí)例,當(dāng)前Activity并不能馬上啟動(dòng)它所包含的Intent,而是在外部執(zhí)行PendingIntent時(shí),調(diào)用Intent。正是由于PendingIntent中保存有當(dāng)前App的context,使它賦予外部App一種能力,使得外部App可以如同當(dāng)前App一樣的執(zhí)行PendingIntent里的Intent,就算在執(zhí)行時(shí)當(dāng)前App已經(jīng)不存在了,也能通過(guò)保存在PendingIntent里的Context照樣執(zhí)行Intent,另外還可以處理Intent執(zhí)行后的操作。常和AlarmManager和NotificationManager一起使用。
至此,一個(gè)簡(jiǎn)單的功能就實(shí)現(xiàn)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android 優(yōu)化之卡頓優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了Android 優(yōu)化之卡頓優(yōu)化的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android開發(fā)中Launcher3常見默認(rèn)配置修改方法總結(jié)
這篇文章主要介紹了Android開發(fā)中Launcher3常見默認(rèn)配置修改方法,結(jié)合實(shí)例形式分析了Android Launcher3的功能與配置修改相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
點(diǎn)九圖片的顯示內(nèi)容區(qū)域應(yīng)作何理解
.9 ,是andriod平臺(tái)的應(yīng)用軟件開發(fā)里的一種特殊的圖片形式,文件擴(kuò)展名為:.9.png;點(diǎn)九圖片的拉伸區(qū)域不難理解,顯示內(nèi)容區(qū)域是怎樣的,接下來(lái)本文為您一一解答,感興趣的朋友可以了解下2013-01-01
Android設(shè)計(jì)模式之Builder模式解析
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之Builder模式解析的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android如何利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Android如何利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Android實(shí)現(xiàn)系統(tǒng)的桌面圖標(biāo)文字的雙行顯示效果
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)的桌面圖標(biāo)文字的雙行顯示效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-10-10

