标签:
本实例通过TimePickerDialog时间选择对话框让用户设置闹钟。并通过AlarmManager全局定时器在指定的时间启动闹钟Activity 。
程序执行效果图:
实例代码:
package com.jph.alarm.test; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; /** * Describe:</br> * </br>简单闹钟提醒 * </br>本实例通过TimePickerDialog时间选择对话框 * </br>让用户设置闹钟并通过AlarmManager全局定时器 * </br>在指定的时间启动闹钟Activity * </br>@author jph * </br>Date:2014.07.29 * */ public class AlarmTest extends Activity { Button setTime; AlarmManager aManager; Calendar currentTime = Calendar.getInstance(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取程序界面的button setTime = (Button) findViewById(R.id.setTime); // 获取AlarmManager对象 aManager = (AlarmManager) getSystemService( Service.ALARM_SERVICE); // 为“设置闹铃”button绑定监听器。setTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Calendar currentTime = Calendar.getInstance(); // 创建一个TimePickerDialog实例,并把它显示出来。 new TimePickerDialog(AlarmTest.this, 0, // 绑定监听器 new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker tp, int hourOfDay, int minute) { // 指定启动AlarmActivity组件 Intent intent = new Intent(); intent.setAction("com.jph.alarm.test.ACTION"); // 创建PendingIntent对象 PendingIntent pi = PendingIntent.getActivity( AlarmTest.this, 0, intent, 0); Calendar c = Calendar.getInstance(); // 依据用户选择时间来设置Calendar对象 c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); // 设置AlarmManager将在Calendar相应的时间启动指定组件 aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); // 显示闹铃设置成功的提示信息 Toast.makeText(AlarmTest.this, "闹铃设置成功啦" , Toast.LENGTH_SHORT).show(); } }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime .get(Calendar.MINUTE), false).show(); } }); } }
package com.jph.alarm.test; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; /** * Describe:</br> * </br>简单闹钟提醒 * </br>本实例通过TimePickerDialog时间选择对话框 * </br>让用户设置闹钟并通过AlarmManager全局定时器 * </br>在指定的时间启动闹钟Activity * </br>@author jph * </br>Date:2014.07.29 * */ public class AlarmTest extends Activity { Button setTime; AlarmManager aManager; Calendar currentTime = Calendar.getInstance(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取程序界面的button setTime = (Button) findViewById(R.id.setTime); // 获取AlarmManager对象 aManager = (AlarmManager) getSystemService( Service.ALARM_SERVICE); // 为“设置闹铃”button绑定监听器。 setTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Calendar currentTime = Calendar.getInstance(); // 创建一个TimePickerDialog实例,并把它显示出来。new TimePickerDialog(AlarmTest.this, 0, // 绑定监听器 new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker tp, int hourOfDay, int minute) { // 指定启动AlarmActivity组件 Intent intent = new Intent(); intent.setAction("com.jph.alarm.test.ACTION"); // 创建PendingIntent对象 PendingIntent pi = PendingIntent.getActivity( AlarmTest.this, 0, intent, 0); Calendar c = Calendar.getInstance(); // 依据用户选择时间来设置Calendar对象 c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); // 设置AlarmManager将在Calendar相应的时间启动指定组件 aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); // 显示闹铃设置成功的提示信息 Toast.makeText(AlarmTest.this, "闹铃设置成功啦" , Toast.LENGTH_SHORT).show(); } }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime .get(Calendar.MINUTE), false).show(); } }); } }
<?xml version="1.0" encoding="utf-8"?
> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jph.alarm.test" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity android:name=".AlarmTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AlarmActivity"> <intent-filter> <action android:name="com.jph.alarm.test.ACTION"/> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> </application> </manifest>
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/5207825.html