Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例
AlarmManager通常用來(lái)開發(fā)手機(jī)鬧鐘,并且它是一個(gè)全局定時(shí)器,可在指定時(shí)間或指定周期啟動(dòng)其他組件(包括Activity,Service,BroadcastReceiver)
獲取AlarmManager對(duì)象:
getSystemService(Service.ALARM_SERVICE)
調(diào)用其中的方式設(shè)置定時(shí)器啟動(dòng)指定組件:
set(int type,long triggerAtTime,PendingIntent operation)設(shè)置在TriggerAtTime時(shí)間啟動(dòng)由operation參數(shù)指定的組件。
“第一個(gè)參數(shù)指定定時(shí)服務(wù)的類型,其值可以使:
ELAPSED_REALTIME:指定從現(xiàn)在開始時(shí)間過(guò)了一定時(shí)間后啟動(dòng)operation對(duì)相應(yīng)的組件。
ELAPSED_REALTIME_WAKEUP:指定從現(xiàn)實(shí)開始時(shí)間過(guò)了一定時(shí)間后啟動(dòng)operation隊(duì)形的組件,即使系統(tǒng)關(guān)機(jī)也會(huì)執(zhí)行operation所對(duì)應(yīng)的組件。
RTC:指定當(dāng)系統(tǒng)調(diào)用System.currentTimeMillls()方法返回值與triggerAtTIme相等時(shí)啟動(dòng)operation所對(duì)應(yīng)的組件。
RTC_WAKEUP:指定當(dāng)系統(tǒng)調(diào)用System.currentTimeMillls()方法返回值與triggerAtTIme相等時(shí)啟動(dòng)operation所對(duì)應(yīng)的組件,即使系統(tǒng)關(guān)機(jī)也會(huì)執(zhí)行operation所對(duì)應(yīng)的組件。”
setInexactRepeationg(int type,long triggerAtTIme,long interval,PendingIntent operation):設(shè)置一個(gè)非精確的周期性任務(wù)。
setRepeating(int type,long triggerAtTIme,long interval,PendingIntent operation):設(shè)置一個(gè)周期性定時(shí)任務(wù)。
cancle(PendingIntent operation)取消AlarmManger定時(shí)服務(wù)
示例:
核心代碼:
public class MainActivity extends Activity { private AlarmManager alarmManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取AlarmManager對(duì)象 alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE); } public void btn(View view) { final Calendar c = Calendar.getInstance(); TimePickerDialog dialog = new TimePickerDialog(this, new OnTimeSetListener() { //延遲意圖(即鬧鐘的意圖) PendingIntent paIntent = PendingIntent.getActivity(MainActivity.this, 1, new Intent(MainActivity.this,SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //設(shè)置你所選擇的時(shí)間 c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); //第一個(gè)參數(shù)指定鬧鐘服務(wù)的類型 //第二個(gè)參數(shù)指定鬧鐘間隔的時(shí)間 //第三個(gè)參數(shù)指跳轉(zhuǎn)鬧鐘的延遲意圖 alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), paIntent); } }, //鬧鐘的初始值(時(shí)和分) c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true); dialog.show(); } }
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.alarmmanager.MainActivity" > <Button android:onClick="btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btntext" /> </RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)android軟件編程有所幫助。
- Android通過(guò)AlarmManager類實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
- Android編程使用AlarmManager設(shè)置鬧鐘的方法
- Android 使用AlarmManager和NotificationManager來(lái)實(shí)現(xiàn)鬧鐘和通知欄
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
- Android編程實(shí)現(xiàn)鬧鐘的方法詳解
- Android實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android使用AlarmManager設(shè)置鬧鐘功能
相關(guān)文章
Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法,實(shí)例分析了Android中加載等待類ProgressDialog的具體使用方法,需要的朋友可以參考下2015-12-12Android長(zhǎng)按imageview把圖片保存到本地的實(shí)例代碼
本文通過(guò)代碼給大家介紹了Android長(zhǎng)按imageview把圖片保存到本地的實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-12-12Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12Android ViewFlipper翻轉(zhuǎn)視圖使用詳解
這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper翻轉(zhuǎn)視圖的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼
彈窗的排隊(duì)執(zhí)行在App中是一個(gè)很常見的應(yīng)用場(chǎng)景,這篇文章為大家介紹了兩個(gè)Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼,感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-08-08Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變
這篇文章主要為大家詳細(xì)介紹了Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android編程實(shí)現(xiàn)圖片拍照剪裁的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片拍照剪裁的方法,涉及Android調(diào)用裁剪工具操作圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12