Tom*_*Tom 87 java android alarmmanager android-alarms android-1.5-cupcake
我需要AlarmManager
在设置20分钟后触发一段代码.
有人可以给我看一下如何AlarmManager
在Android中使用的示例代码吗?
我已经玩了几天的代码,它只是不起作用.
Com*_*are 106
"一些示例代码"并非如此简单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);
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我正在使用setRepeating()
.如果你想要一次性警报,你就可以使用set()
.请务必在与初始参数中使用的时间基准相同的时间内为警报提供时间set()
.在我上面的例子中,我正在使用AlarmManager.ELAPSED_REALTIME_WAKEUP
,所以我的时间基准是SystemClock.elapsedRealtime()
.
Def*_*ult 63
android示例代码中有一些很好的例子
\ Android的SDK \样品\机器人-10\ApiDemos\SRC\COM \示例\机器人\的API \应用
要检查的是:
首先,你需要一个接收器,它可以在触发时听到你的警报.将以下内容添加到AndroidManifest.xml文件中
<receiver android:name=".MyAlarmReceiver" />
Run Code Online (Sandbox Code Playgroud)
然后,创建以下类
public class MyAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,要触发警报,请使用以下内容(例如,在您的主要活动中):
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);
Run Code Online (Sandbox Code Playgroud)
.
或者,更好的是,创建一个处理它的类,并像这样使用它
Bundle bundle = new Bundle();
// add extras here..
MyAlarm alarm = new MyAlarm(this, bundle, 30);
Run Code Online (Sandbox Code Playgroud)
这样,你就可以在一个地方(不要忘记编辑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();
}
}
Run Code Online (Sandbox Code Playgroud)
您需要做的是首先创建您需要安排的意图.然后获取该intent的pendingIntent.您可以安排活动,服务和广播.安排活动,例如MyActivity:
Intent i = new Intent(getApplicationContext(), MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),3333,i,
PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
将此pendingIntent提供给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);
Run Code Online (Sandbox Code Playgroud)
现在MyActivity将在应用程序启动5秒后启动,无论您停止应用程序还是设备进入睡眠状态(由于RTC_WAKEUP选项).您可以阅读完整的示例代码调度活动,服务和广播#Android
归档时间: |
|
查看次数: |
80528 次 |
最近记录: |