首先AndroidManifest.xml
<!-- 在其它应用上显示的权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- android:process=":remote" 新开一个进程 -->
<receiver android:name=".AlarmReceiver" android:process=":remote" />
AlarmTool.java
package com.zc.test;
import java.util.Calendar;
import java.util.Locale;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmTool{
//设置闹钟
public void setAlarm(final Context context){
Log.d("zc", "设置闹钟");
//获取日历实例对象
final Calendar calendar = Calendar.getInstance(Locale.CHINESE);
//设置日历时间为当前系统时间
calendar.setTimeInMillis(System.currentTimeMillis());
//MyTimePickerDialog为 自定义TimePickerDialog 为了解决TimePickerDialog的bug
MyTimePickerDialog mt = new MyTimePickerDialog(context,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int hourOfDay, int minute) {
// TODO onTimeSet();
//设置日历的时间,主要是让日历的年月日和当前同步
calendar.setTimeInMillis(System.currentTimeMillis());
//设置时间
// calendar.add(Calendar.SECOND, 5);
// calendar.add(Calendar.MINUTE, 30);
//设置日历的小时和分钟
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
//将秒和毫秒设置为0
// calendar.set(Calendar.SECOND, 0);
// calendar.set(Calendar.MILLISECOND, 0);
//建立Intent和PendingIntent来调用闹钟管理器
Intent intent = new Intent(context,AlarmReceiver.class);
//pendingIntent 待定的Intent 不会立刻执行
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//获取闹钟管理器
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//设置闹钟,AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0.
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
//设置闹钟 setRepeating 重复闹钟
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
Toast.makeText(context, "设置闹钟的时间为:"+String.valueOf(hourOfDay)+":"+String.valueOf(minute), Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true);
mt.setButton(DialogInterface.BUTTON_NEUTRAL, "cancle", new OnClickListener() {
//取消按钮
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
mt.show();
}
//TODO timeOutShow();
public void timeOutShow(Context con){
Log.i("zc", "timeOutShow");
AlertDialog.Builder ader = new Builder(con);
ader.setTitle("Time Out:")
.setMessage("时间用完了")
.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
//设置Type属性才能在Receiver中调用
AlertDialog ad = ader.create();
ad.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
ad.show();
}
//自定义TimePickerDialog,
//Android的版本bug,TimePickerDialog在结束时必定会调用onTimeSet();
//点击按钮,或者空白处都会调用onTimeSet(),点击按钮会多调用一次,即效果为有两个同时间的闹钟
class MyTimePickerDialog extends TimePickerDialog{
public MyTimePickerDialog(Context context, OnTimeSetListener onTimeSetListener,
int hourOfDay, int minute, boolean is24HourView) {
super(context, onTimeSetListener, hourOfDay, minute, is24HourView);
}
//复写onStop(); 注掉super.onStop(); 否则onTimeSet()会执行两次
@Override
protected void onStop(){
//super.onStop();
}
}
}
package com.zc.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context con, Intent arg1) {
// TODO Auto-generated method stub
Log.i("zc", "onReceive");
Toast.makeText(con, "alarmTimeOut", Toast.LENGTH_SHORT).show();
new AlarmTool().timeOutShow(con);
}
}
如果要固定闹钟时间,比如30min
直接用onClickListener(); 不需要设置时间onTimeSet();