从AlarmManager
设置开始20分钟后,我需要触发一段代码。
有人可以向我展示如何AlarmManager
在ِ Android中使用示例代码吗?
我已经玩了几天的代码了,只是行不通。
Answers:
涉及到“一些示例代码”并不是那么容易AlarmManager
。
以下是显示的设置的代码段AlarmManager
:
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
在此示例中,我正在使用setRepeating()
。如果您想要一键式警报,则只需使用set()
。确保给警报的启动时间与您在的初始参数中使用的时间相同set()
。在上面的示例中,我正在使用AlarmManager.ELAPSED_REALTIME_WAKEUP
,因此我的时基为SystemClock.elapsedRealtime()
。
android示例代码中有一些很好的例子
。\ android-sdk \ samples \ android-10 \ ApiDemos \ src \ com \ example \ android \ apis \ app
要检查的是:
首先,您需要一个接收器,它可以在触发警报时监听您的声音。将以下内容添加到您的AndroidManifest.xml文件中
<receiver android:name=".MyAlarmReceiver" />
然后,创建以下类
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
}
}
然后,要触发警报,请使用以下命令(例如在您的主要活动中):
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
。
或者更好的是,创建一个处理所有内容的类并像这样使用它
Bundle bundle = new Bundle();
// add extras here..
MyAlarm alarm = new MyAlarm(this, bundle, 30);
这样一来,您就可以将所有内容都放在一个地方(不要忘了编辑AndroidManifest.xml
)
public class MyAlarm extends BroadcastReceiver {
private final String REMINDER_BUNDLE = "MyReminderBundle";
// this constructor is called by the alarm manager.
public MyAlarm(){ }
// you can use this constructor to create the alarm.
// Just pass in the main activity as the context,
// any extras you'd like to get later when triggered
// and the timeout
public MyAlarm(Context context, Bundle extras, int timeoutInSeconds){
AlarmManager alarmMgr =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyAlarm.class);
intent.putExtra(REMINDER_BUNDLE, extras);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, timeoutInSeconds);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
pendingIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
// here you can get the extras you passed in when creating the alarm
//intent.getBundleExtra(REMINDER_BUNDLE));
Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
}
}
您需要做的是首先创建需要计划的意图。然后获取该意图的pendingIntent。您可以安排活动,服务和广播。安排活动,例如MyActivity:
Intent i = new Intent(getApplicationContext(), MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),3333,i,
PendingIntent.FLAG_CANCEL_CURRENT);
将此alarmIntent赋予alarmManager:
//getting current time and add 5 seconds in it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
//registering our pending intent with alarmmanager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pi);
现在,无论您停止应用程序还是设备进入睡眠状态(由于RTC_WAKEUP选项), MyActivity都会在应用程序启动5秒后启动。您可以阅读完整的示例代码调度活动,服务和广播#Android
当您想从Alarmmanager调用服务时,一些示例代码:
PendingIntent pi;
AlarmManager mgr;
mgr = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(DataCollectionActivity.this, HUJIDataCollectionService.class);
pi = PendingIntent.getService(DataCollectionActivity.this, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , 1000, pi);
您不必询问用户权限。
AlarmManager用于在特定时间触发一些代码。
要启动警报管理器,您需要首先从系统获取实例。然后传递PendingIntent,它将在您指定的将来时间执行
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
int interval = 8000; //repeat interval
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
使用警报管理器时需要小心。通常,警报管理器不会在一分钟前重复。同样在低功率模式下,持续时间最多可以增加15分钟。