android开发步步为营之18:闹钟AlarmManager的使用
2014-09-23 17:51
387 查看
public class android.app.AlarmManager
extends Object
Class Overview
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting
the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
public final class android.app.PendingIntent
extends Object implements Parcelable
Class Overview
A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int); the returned object can be handed
to other applications so that they can perform the action you described on your behalf at a later time.
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build
the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
(1)、设置定时器
private Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, _year);
calendar.set(Calendar.MONTH, _month - 1);// 系统是0-11
calendar.set(Calendar.DAY_OF_MONTH, _day);
calendar.set(Calendar.HOUR_OF_DAY, _hour);
calendar.set(Calendar.MINUTE, _minute);
calendar.set(Calendar.SECOND, _second);
calendar.set(Calendar.MILLISECOND, 0);
//新建一个意图,目的地是CallAlarm广播接收器
Intent intentalarm = new Intent(FigoMemoAddActivity.this,
CallAlarm.class);
intentalarm.putExtra("_id", id);
//新建一个待处理(将来时)的意图
PendingIntent operation;
//待处理(将来时)的意图,发出一个广播,触发意图intentalarm的CallAlarm.java广播接收器
operation = PendingIntent.getBroadcast(
FigoMemoAddActivity.this, Integer.parseInt(id),
intentalarm, 0);
// }
//设置定时器(闹钟)
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
operation);
(2)、广播接收器接收闹钟触发的intent
public class CallAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String _id=arg1.getStringExtra("_id");//获取备忘录编号
Log.i("CallAlarm", "获取到的_id:"+_id);
Intent intent = new Intent(arg0, AlarmAlertActivity.class);
Bundle bundle = new Bundle();
bundle.putString("STR_CALLER", "");
bundle.putString("_id", _id);//将备忘录编号传递下去
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(intent);
}
}
(3)、AndroidManifest.xml注册
<application android:icon="@drawable/figomemo"
android:label="@string/app_name">
<receiver android:name=".CallAlarm" android:process=":remote"></receiver>
</application>
extends Object
Class Overview
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting
the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
public final class android.app.PendingIntent
extends Object implements Parcelable
Class Overview
A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int); the returned object can be handed
to other applications so that they can perform the action you described on your behalf at a later time.
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build
the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
(1)、设置定时器
private Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, _year);
calendar.set(Calendar.MONTH, _month - 1);// 系统是0-11
calendar.set(Calendar.DAY_OF_MONTH, _day);
calendar.set(Calendar.HOUR_OF_DAY, _hour);
calendar.set(Calendar.MINUTE, _minute);
calendar.set(Calendar.SECOND, _second);
calendar.set(Calendar.MILLISECOND, 0);
//新建一个意图,目的地是CallAlarm广播接收器
Intent intentalarm = new Intent(FigoMemoAddActivity.this,
CallAlarm.class);
intentalarm.putExtra("_id", id);
//新建一个待处理(将来时)的意图
PendingIntent operation;
//待处理(将来时)的意图,发出一个广播,触发意图intentalarm的CallAlarm.java广播接收器
operation = PendingIntent.getBroadcast(
FigoMemoAddActivity.this, Integer.parseInt(id),
intentalarm, 0);
// }
//设置定时器(闹钟)
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
operation);
(2)、广播接收器接收闹钟触发的intent
public class CallAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String _id=arg1.getStringExtra("_id");//获取备忘录编号
Log.i("CallAlarm", "获取到的_id:"+_id);
Intent intent = new Intent(arg0, AlarmAlertActivity.class);
Bundle bundle = new Bundle();
bundle.putString("STR_CALLER", "");
bundle.putString("_id", _id);//将备忘录编号传递下去
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(intent);
}
}
(3)、AndroidManifest.xml注册
<application android:icon="@drawable/figomemo"
android:label="@string/app_name">
<receiver android:name=".CallAlarm" android:process=":remote"></receiver>
</application>
相关文章推荐
- Android 定时执行任务 AlarmManager的使用(可用于闹钟开发)
- Android开发(18)--NotiFication详解与使用
- android开发步步为营之42:listview的使用(上篇:显示普通文本)
- android开发步步为营之84:selector与shape的结合使用
- Android闹钟 AlarmManager的使用
- Android开发(18)--NotiFication详解与使用
- Android闹钟 AlarmManager的使用
- android开发步步为营之85:RecyclerView简单使用
- (4.1.32.1)Android闹钟 AlarmManager的使用
- android开发笔记之 AlarmManager(闹钟服务)
- Android闹钟 AlarmManager的使用
- 步步为营_Android开发课[18]_用户界面之Dialog(对话框)
- android开发步步为营之44:使用WebView打开web页面
- Android闹钟 AlarmManager的使用
- Android 使用AlarmManager设置闹钟
- Android实用闹钟开发(三)---AlarmManager和Calendar
- android开发步步为营之47:使用viewflipper实现屏幕的切换
- android开发之AlarmManager的使用方法
- android开发步步为营之98:android studio使用技巧之一(如何清理不需要的文件)