我需要触发一块代码后20分钟 AlarmManager 被设定的。

可以有人告诉我的代码样本有关如何使用 AlarmManager 在那一刻。

我已经玩弄一些代码几天,它是不会的工作。

有帮助吗?

解决方案

“一些示例代码”,当涉及到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()

这里是一个更大的样本项目示出了这种技术。

其他提示

有一些很好的例子在代码样本

.\安卓-sdk\样\安卓-10\ApiDemos\src\com\例\安卓\api\应用程序

那个检查是:

  • AlarmController.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();
     }
}

您需要做的就是先创建你需要安排的意图是什么。然后,获得意向的PendingIntent。您可以安排活动,服务和广播。 要安排一个活动e.g MyActivity:

  Intent i = new Intent(getApplicationContext(), MyActivity.class);
  PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),3333,i,
  PendingIntent.FLAG_CANCEL_CURRENT);

给这个的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);

现在MyActivity将应用程序启动的5秒后推出的,不管你停你的应用程序或设备处于睡眠状态去(由于RTC_WAKEUP选项)。 你可以阅读完整的示例代码调度活动,服务和广播#Android

我想发表评论,但<50代表,所以这里去。友好的提醒,如果你在5.1或以上运行,并且使用的间隔不到一分钟,出现这种情况:

Suspiciously short interval 5000 millis; expanding to 60 seconds

请参阅此处

当你想调用从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);

你不必问userpermissions。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top