android开发步步为营之18:闹钟AlarmManager的使用

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值