Android开发实例之闹钟提醒

      本实例通过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:
*
简单闹钟提醒 *
本实例通过TimePickerDialog时间选择对话框 *
让用户设置闹钟并通过AlarmManager全局定时器 *
在指定的时间启动闹钟Activity *
@author jph *
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); // 获取程序界面的按钮 setTime = (Button) findViewById(R.id.setTime); // 获取AlarmManager对象 aManager = (AlarmManager) getSystemService( Service.ALARM_SERVICE); // 为“设置闹铃”按钮绑定监听器。 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:
*
简单闹钟提醒 *
本实例通过TimePickerDialog时间选择对话框 *
让用户设置闹钟并通过AlarmManager全局定时器 *
在指定的时间启动闹钟Activity *
@author jph *
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); // 获取程序界面的按钮 setTime = (Button) findViewById(R.id.setTime); // 获取AlarmManager对象 aManager = (AlarmManager) getSystemService( Service.ALARM_SERVICE); // 为“设置闹铃”按钮绑定监听器。 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



	
	
		
			
				
				
			
		
		
		    
			    
			    
			
		
	

 



你可能感兴趣的:(Android开发)