本文实例讲述了AndroID编程使用AlarmManager设置闹钟的方法。分享给大家供大家参考,具体如下:
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 创建时间:2010 Jul 8,2010 3:03:19 PM * 类说明 */public class AlamrReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context,Intent intent) { // Todo auto-generated method stub Toast.makeText(context,"闹钟时间到",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,intent,0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);//设置闹钟 am.setRepeating(AlarmManager.RTC_WAKEUP,(10*1000),pi);//重复设置 tv.setText("设置的闹钟时间为:"+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); 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="设置闹钟" 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:关于AndroIDManifest.xml文件相关属性功能可参考本站在线工具:
AndroID Manifest功能与权限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程使用AlarmManager设置闹钟的方法全部内容,希望文章能够帮你解决Android编程使用AlarmManager设置闹钟的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)