闹钟的使用
第一步:注册闹钟(AlarmManager,PendingIntent,Intent,闹钟的时间)
第二步:注册闹钟广播接受者(BroadcastReceiver)
第三步:处理闹钟的时间(比如:打开一个应用,提示用户时间到了 etc....)
=========AlarmAlert.java==========
private void updateLayout() {
。。。。。。。。。。
。。。。。。。。。。
/* snooze behavior: pop a snooze confirmation view, kick alarm
manager. */
Button snooze = (Button) findViewById(R.id.snooze);
snooze.requestFocus();
snooze.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
snooze();
}
});
........
........
}
// Attempt to snooze this alert.
private void snooze() {
final String snooze =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
int snoozeMinutes = Integer.parseInt(snooze);
final long snoozeTime = System.currentTimeMillis()
+ (1000 * 60 * snoozeMinutes); //计算睡眠时间
Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarm.id, snoozeTime);//保存睡眠的时间
// Get the display time for the snooze and update the notification.
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(snoozeTime);
。。。。
。。。。
。。。。
// Notify the user that the alarm has been snoozed.//通知给广播接收者 AlarmReceiver.class 并处理闹钟时间
Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
PendingIntent broadcast =
PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
NotificationManager nm = getNotificationManager();
。。。。。
。。。。。。。。。。。。。
stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
finish();
}
=========Alarm.java==========
static void saveSnoozeAlert(final Context context, final int id,
final long time) {
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
SharedPreferences.Editor ed = prefs.edit();
if (id == -1) {
clearSnoozePreference(ed);
} else {
ed.putInt(PREF_SNOOZE_ID, id);
ed.putLong(PREF_SNOOZE_TIME, time); //保存提示的时间
ed.commit();
}
// Set the next alert after updating the snooze.
setNextAlert(context);
}
/**
* If there is a snooze set, enable it in AlarmManager
* @return true if snooze is set
*/
private static boolean enableSnoozeAlert(final Context context) {
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
int id = prefs.getInt(PREF_SNOOZE_ID, -1);
if (id == -1) {
return false;
}
long time = prefs.getLong(PREF_SNOOZE_TIME, -1); //取出设置闹钟的时间
// Get the alarm from the db.
final Alarm alarm = getAlarm(context.getContentResolver(), id);
// The time in the database is either 0 (repeating) or a specific time
// for a non-repeating alarm. Update this value so the AlarmReceiver
// has the right time to compare.
alarm.time = time; //将时间放入数据库中
enableAlert(context, alarm, time);
return true;
}
/**
* Sets alert in AlarmManger and StatusBar. This is what will
* actually launch the alert when the alarm triggers.
*
* @param alarm Alarm.
* @param atTimeInMillis milliseconds since epoch
*/
private static void enableAlert(Context context, final Alarm alarm,
final long atTimeInMillis) {
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //接受广播的闹钟的广播意图 Action:“ALARM_ALERT_ACTION”
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); //设置闹钟
/**
//闹钟的广播接受者
* Glue class: connects AlarmAlert IntentReceiver to AlarmAlert
* activity. Passes through Alarm ID.
*/
public class AlarmReceiver extends BroadcastReceiver {
.......
.....
.......
/* launch UI, explicitly stating that this is not due to user action
* so that the current app's notification management is not disturbed */
Intent alarmAlert = new Intent(context, c);
alarmAlert.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmAlert);
...
........
// Play the alarm alert and vibrate the device.
Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
context.startService(playAlarm);
...........
.....
}
分享到:
相关推荐
android 闹钟 源代码, 做了一些修改,相信一些朋友会有用
Android 闹钟源码.zip
android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动
Android闹钟,可以设置每天,也可以重复设置多个,并且有提醒界面,也可以自己修改提醒音乐,有需要的可以下载,这是软件中的一个闹钟功能
Android 闹钟app 源码 满足 学习练手的需求,如果有别的需求可以定制开发
Android 闹钟 倒计时源代码,一个运行于Android手机的BrewClock闹钟源代码,可以学习到一些秒表、闹钟、倒计时相关的android编程技巧,源代码也是比较完整的。
android闹钟修改后的可用源代码 可以用来定时等等
Android闹钟源码
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包。由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤醒Service发送心跳包。 以下是...
Android 闹钟例子
Android 闹钟源码
android闹钟源码闹钟程序源码15个合集: AlarmClock.tar.gz AlarmTest.zip AndroidRTC-master.zip Android自动开关机实现.pdf Android自动开关机实现.rar ClockPackage.apk-af47e27b7952e0ecccc9b005352d19d20c67eb...
一个Android闹钟demo,基本实现了闹钟的基本功能,感兴趣的同学可以看一下
一款基于android的简易闹钟源代码,非常适合初学者学习
android 闹钟服务,最简单的闹钟使用方法
闹钟是基于Android studio开发的一款打扰睡觉的app,功能实现闹铃、添加、删除