Android編程使用AlarmManager設(shè)置鬧鐘的方法
本文實(shí)例講述了Android編程使用AlarmManager設(shè)置鬧鐘的方法。分享給大家供大家參考,具體如下:
package com.Aina.Android; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * com.Aina.Android * Pro_AlarmManager * @author Aina.huang E-mail: 674023920@qq.com * @version 創(chuàng)建時(shí)間:2010 Jul 8, 2010 3:03:19 PM * 類說(shuō)明 */ public class AlamrReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "鬧鐘時(shí)間到", Toast.LENGTH_LONG).show(); } }
package com.Aina.Android; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class Test extends Activity { /** Called when the activity is first created. */ private TextView tv = null; private Button btn_set = null; private Button btn_cel = null; private Calendar c = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) this.findViewById(R.id.TextView); btn_set = (Button) this.findViewById(R.id.Button01); btn_cel = (Button) this.findViewById(R.id.Button02); c = Calendar.getInstance(); btn_set.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(Test.this,new TimePickerDialog.OnTimeSetListener(){ public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(Test.this,AlamrReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//設(shè)置鬧鐘 am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (10*1000), pi);//重復(fù)設(shè)置 tv.setText("設(shè)置的鬧鐘時(shí)間為:"+hourOfDay+":"+minute); } },hour,minute,true).show(); } }); btn_cel.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Test.this,AlamrReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.cancel(pi); tv.setText("鬧鐘取消"); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:id="@+id/TextView" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="設(shè)置鬧鐘" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <Button android:text="取消鬧鐘" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Aina.Android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Test" 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=".AlamrReceiver" android:process=":remote"></receiver> </application> </manifest>
PS:關(guān)于AndroidManifest.xml文件相關(guān)屬性功能可參考本站在線工具:
Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android通過(guò)AlarmManager類實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
- Android 使用AlarmManager和NotificationManager來(lái)實(shí)現(xiàn)鬧鐘和通知欄
- Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例
- 簡(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使用Xutils3進(jìn)行斷點(diǎn)下載的實(shí)例
在本篇內(nèi)容中小編給各位整理了關(guān)于Android使用Xutils3進(jìn)行斷點(diǎn)下載的實(shí)例以及相關(guān)代碼,需要的朋友們參考下。2019-07-07Kotlin實(shí)現(xiàn)多函數(shù)接口的簡(jiǎn)化調(diào)用
這篇文章主要為大家詳細(xì)介紹了Kotlin實(shí)現(xiàn)多函數(shù)接口的簡(jiǎn)化調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android仿美團(tuán)網(wǎng)、大眾點(diǎn)評(píng)購(gòu)買框懸浮效果修改版
這篇文章主要為大家詳細(xì)介紹了Android仿美團(tuán)網(wǎng)、大眾點(diǎn)評(píng)購(gòu)買框懸浮效果的修改版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android利用DownloadManager實(shí)現(xiàn)文件下載
這篇文章主要為大家詳細(xì)介紹了Android利用DownloadManager實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度)
本篇文章主要介紹了詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Android編程判斷網(wǎng)絡(luò)是否可用及調(diào)用系統(tǒng)設(shè)置項(xiàng)的方法
這篇文章主要介紹了Android編程判斷網(wǎng)絡(luò)是否可用及調(diào)用系統(tǒng)設(shè)置項(xiàng)的方法,涉及Android針對(duì)網(wǎng)絡(luò)連接的判定及屬性設(shè)置的調(diào)用,需要的朋友可以參考下2016-03-03Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼(11)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11詳解Android XML中引用自定義內(nèi)部類view的四個(gè)why
本篇文章主要介紹了詳解Android XML中引用自定義內(nèi)部類view,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12