Вопрос по android, java – Android: как использовать AlarmManager

84

Мне нужно вызвать блок кода через 20 минут послеAlarmManager быть установленным

Может кто-нибудь показать мне пример кода о том, как использоватьAlarmManager в & # x650; Android?

Я несколько дней играл с кодом, и он просто не работал.

Ваш Ответ

5   ответов
62

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

  • AlarmController.java
  • OneShotAlarm.java

Error: User Rate Limit Exceeded

<receiver android:name=".MyAlarmReceiver" />

Error: User Rate Limit Exceeded

public class MyAlarmReceiver extends BroadcastReceiver { 
     @Override
     public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
     }
}

Error: User Rate Limit Exceeded

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);

.


Error: User Rate Limit Exceeded

Bundle bundle = new Bundle();
// add extras here..
MyAlarm alarm = new MyAlarm(this, bundle, 30);

Error: User Rate Limit ExceededAndroidManifest.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();
     }
}
Error: User Rate Limit Exceededstackoverflow.com/questions/2844274/…
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
3

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);

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
8

Error: User Rate Limit ExceededTo schedule an activity e.g MyActivity:

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

Error: User Rate Limit Exceeded

  //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);

Error: User Rate Limit Exceededlaunched after 5 seconds of the application launch, no matter you stop your application or device went in sleep stateError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
104

Error: User Rate Limit ExceededAlarmManager.

Error: User Rate Limit ExceededAlarmManager:

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);

Error: User Rate Limit ExceededsetRepeating(), Если вы хотите однократный сигнал тревоги, вы просто используетеset()Error: User Rate Limit Exceededset()Error: User Rate Limit ExceededAlarmManager.ELAPSED_REALTIME_WAKEUPтак что моя временная базаSystemClock.elapsedRealtime().

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Tom
Error: User Rate Limit Exceededgroups.google.com/group/cw-androidError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
4

Error: User Rate Limit Exceeded

Suspiciously short interval 5000 millis; expanding to 60 seconds

Error: User Rate Limit ExceededВот.

Похожие вопросы