Android:如何使用 AlarmManager

新手上路,请多包涵

我需要在设置 AlarmManager 20 分钟后触发一段代码。

有人可以告诉我如何在 ِAndroid 中使用 AlarmManager 的示例代码吗?

几天来我一直在研究一些代码,但它就是行不通。

原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 380
2 个回答

当涉及到 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()

这是一个更大的示例项目,展示了这种技术。

原文由 CommonsWare 发布,翻译遵循 CC BY-SA 3.0 许可协议

android示例代码中有一些很好的例子

.\android-sdk\samples\android-10\ApiDemos\src\com\example\android\apis\app

要检查的是:

  • 报警控制器.java
  • OneShotAlarm.java

首先,您需要一个接收器,它可以在警报被触发时收听您的警报。将以下内容添加到您的 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();
     }
}

原文由 default 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题