Android: How to use AlarmManager

前端 未结 6 1012
时光取名叫无心
时光取名叫无心 2020-11-22 06:40

I need to trigger a block of code after 20 minutes from the AlarmManager being set.

Can someone show me sample code on how to use an AlarmManager<

6条回答
  •  再見小時候
    2020-11-22 07:20

    Some sample code when you want to call a service from the 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);
    

    You dont have to ask userpermissions.

提交回复
热议问题