闹钟DEMO精简化,去掉了铃声,周/时/分,定时到点跳转某动作
技术:Alarm+广播+BroadcastReceiver
概述
手机本地时间设定,按照闹钟模式精简化,到点可做某动作,可精确到周时分秒。
详细
概述
顾名思义,时间设定,时间一到,某动作就实现了!可用于手机自动某动作,智能远程控制,闹钟等等。
详细
采用闹钟模式的时间设定,精确到每天、某一天,时间一到发生的事件可重复或单一。
这个Demo是设置定时提醒的功能,即设置几点几分后提醒,用的是给系统设置个时间点,当系统时间到达设置的时间点的时候就会给我们发送一个广播,然后达到时间提醒功能,具体的时间提醒可以是闹钟响铃、跳转页面、发送数据、手机某软件自动打开等等动作。
一、运行效果
二、实现过程
①、创建广播接收AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver { /** If the alarm is older than STALE_WINDOW seconds, ignore. It is probably the result of a time or timezone change */ private final static int STALE_WINDOW = 60 * 30; int hour,min,second;
②、广播在Manifest.xml中配置
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
③、设置闹钟:添加、修改、删除(部分核心代码)
// Get each preference so we can retrieve the value later. mLabel = (EditTextPreference) findPreference("label"); mLabel.setOnPreferenceChangeListener( new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference p, Object newValue) { String val = (String) newValue; // Set the summary based on the new label. p.setSummary(val); if (val != null && !val.equals(mLabel.getText())) { // Call through to the generic listener. return SetAlarm.this.onPreferenceChange(p, newValue); } return true; } }); mEnabledPref = (CheckBoxPreference) findPreference("enabled"); mEnabledPref.setOnPreferenceChangeListener( new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference p, Object newValue) { // Pop a toast when enabling alarms. if (!mEnabledPref.isChecked()) { popAlarmSetToast(SetAlarm.this, mHour, mMinutes, mRepeatPref.getDaysOfWeek()); } return SetAlarm.this.onPreferenceChange(p, newValue); } }); mTimePref = findPreference("time"); mRepeatPref = (RepeatPreference) findPreference("setRepeat"); mRepeatPref.setOnPreferenceChangeListener(this); Intent i = getIntent(); mId = i.getIntExtra(Alarms.ALARM_ID, -1); if (Log.LOGV) { Log.v("In SetAlarm, alarm id = " + mId); } Alarm alarm = null; if (mId == -1) { // No alarm id means create a new alarm. alarm = new Alarm(); } else { /* load alarm details from database */ alarm = Alarms.getAlarm(getContentResolver(), mId); // Bad alarm, bail to avoid a NPE. if (alarm == null) { finish(); return; } } mOriginalAlarm = alarm; updatePrefs(mOriginalAlarm); // We have to do this to get the save/cancel buttons to highlight on // their own. getListView().setItemsCanFocus(true); // Attach actions to each button. Button b = (Button) findViewById(R.id.alarm_save); b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { saveAlarm(); finish(); } }); final Button revert = (Button) findViewById(R.id.alarm_revert); revert.setEnabled(false); revert.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int newId = mId; updatePrefs(mOriginalAlarm); // "Revert" on a newly created alarm should delete it. if (mOriginalAlarm.id == -1) { Alarms.deleteAlarm(SetAlarm.this, newId); } else { saveAlarm(); } revert.setEnabled(false); } }); b = (Button) findViewById(R.id.alarm_delete); if (mId == -1) { b.setEnabled(false); } else { b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { deleteAlarm(); } }); } // The last thing we do is pop the time picker if this is a new alarm. if (mId == -1) { // Assume the user hit cancel mTimePickerCancelled = true; showTimePicker(); } } private void updatePrefs(Alarm alarm) { mId = alarm.id; mEnabledPref.setChecked(alarm.enabled); mLabel.setText(alarm.label); mLabel.setSummary(alarm.label); mHour = alarm.hour; mMinutes = alarm.minutes; mRepeatPref.setDaysOfWeek(alarm.daysOfWeek); updateTime(); } public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // onTimeSet is called when the user clicks "Set" mTimePickerCancelled = false; mHour = hourOfDay; mMinutes = minute; updateTime(); // If the time has been changed, enable the alarm. mEnabledPref.setChecked(true); // Save the alarm and pop a toast. popAlarmSetToast(this, saveAlarmAndEnableRevert()); } private void updateTime() { if (Log.LOGV) { Log.v("updateTime " + mId); } mTimePref.setSummary(Alarms.formatTime(this, mHour, mMinutes, mRepeatPref.getDaysOfWeek())); } private long saveAlarmAndEnableRevert() { // Enable "Revert" to go back to the original Alarm. final Button revert = (Button) findViewById(R.id.alarm_revert); revert.setEnabled(true); return saveAlarm(); } private long saveAlarm() { Alarm alarm = new Alarm(); alarm.id = mId; alarm.enabled = mEnabledPref.isChecked(); alarm.hour = mHour; alarm.minutes = mMinutes; alarm.daysOfWeek = mRepeatPref.getDaysOfWeek(); alarm.label = mLabel.getText(); long time; if (alarm.id == -1) { time = Alarms.addAlarm(this, alarm); // addAlarm populates the alarm with the new id. Update mId so that // changes to other preferences update the new alarm. mId = alarm.id; } else { time = Alarms.setAlarm(this, alarm); } return time; } private void deleteAlarm() { new AlertDialog.Builder(this) .setTitle(getString(R.string.delete_alarm)) .setMessage(getString(R.string.delete_alarm_confirm)) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int w) { Alarms.deleteAlarm(SetAlarm.this, mId); finish(); } }) .setNegativeButton(android.R.string.cancel, null) .show(); } static void popAlarmSetToast(Context context, int hour, int minute, Alarm.DaysOfWeek daysOfWeek) { popAlarmSetToast(context, Alarms.calculateAlarm(hour, minute, daysOfWeek) .getTimeInMillis()); }
④、时间点响应实现某动作,AlarmReceiver.java类实现
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS aaa"); Log.v("AlarmReceiver.onReceive() id " + alarm.id + " setFor " + format.format(new Date(alarm.time))); System.out.println("时间到啦!!"+format.format(new Date(alarm.time))); String time = format.format(new Date(alarm.time)); String[] splittime=time.split(":"); System.out.println("北京时间:"+splittime[0]+"时"+splittime[1]+"分");
如图:
三、项目结构图
四、补充
基于Eclipse平台做的demo,简单粗暴,蛮好用的!
基本实现思路:
Android的闹钟实现机制很简单, 只需要调用AlarmManager.set()将闹铃时间记录到系统中,当闹铃时间到后,系统会给应用程序发送广播,我们只需要去注册广播接收器就可以了。
本实例支付的费用只是购买源码的费用,如有疑问欢迎在文末留言交流,如需作者在线代码指导、定制等,在作者开启付费服务后,可以点击“购买服务”进行实时联系,请知悉,谢谢
手机上随时阅读、收藏该文章 ?请扫下方二维码