**最近项目中需要完成一个需求:实现类似于闹钟的功能,能够在指定的时间唤醒。
于是开始使用alarmManager。。。。查了查别人的博客之后
开始的代码是这么写的:**
private void setTime() {
// 当前设备上的系统时间
final Calendar cal = Calendar.getInstance();
// 弹出设置时间的窗口
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = new Intent(context, AlarmNotifyAcitivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0,
intent, 0);
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR, hourOfDay);
setCalendar.set(Calendar.MINUTE, minute);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
setCalendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
timeSetBtn.setText(setCalendar.get(Calendar.HOUR) + ":"
+ setCalendar.get(Calendar.MINUTE));
Toast.makeText(context, "闹铃设置成功", Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
.show();
}
intent中指定的activity用来处理逻辑
但是现实是–根本没有任何的反应!!
查了查官方的api发现,android在alarm设定之后,启动时发送的是广播,使用getActivity得到的pendingintent是无法实现效果的,并且需要一个继承自广播接收器的类接收广播,于是更改代码:
private void setTime() {
// 当前设备上的系统时间
final Calendar cal = Calendar.getInstance();
// 弹出设置时间的窗口
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = new Intent(context, AlarmNotifyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0,
intent, 0);
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR, hourOfDay);
setCalendar.set(Calendar.MINUTE, minute);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
setCalendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
timeSetBtn.setText(setCalendar.get(Calendar.HOUR) + ":"
+ setCalendar.get(Calendar.MINUTE));
Toast.makeText(context, "闹铃设置成功", Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
.show();
}
用来接收广播的类:
public class AlarmNotifyReceiver extends BroadcastReceiver {
private MediaPlayer notice;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "启动闹钟", Toast.LENGTH_SHORT).show();
notice = MediaPlayer.create(context,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
notice.setLooping(true);
try {
notice.prepare();
notice.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("日常提醒闹钟").setMessage("请执行操作")
.setPositiveButton("好哒~~", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notice.stop();
}
});
AlertDialog dialog=builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
}
这里我做的是显示一个alert,注意如果不设置settype再运行时会报错!!!注意如果不设置settype再运行时会报错!!!注意如果不设置settype再运行时会报错!!!重要的事情说三遍。。。
但是设置之后运行,依然还是报错:-。-
需要注册权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
ok,大功告成!!
但是有点需要特别提醒,android自从4.4之后,唤醒的时间未必是完全准确的,系统会将某一段时间内需要唤醒的放在一个时间唤醒,而这个时间未必是你设定的时间。