码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发实例之闹钟提醒

时间:2016-02-22 19:12:49      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

      本实例通过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(); } }); } }


AndroidManifest.xml

<?

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>




Android开发实例之闹钟提醒

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5207825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!