1- package de .vogella .android .ownservice .local ;
2-
3- import java .util .Calendar ;
4-
5- import android .app .AlarmManager ;
6- import android .app .PendingIntent ;
7- import android .content .BroadcastReceiver ;
8- import android .content .Context ;
9- import android .content .Intent ;
10-
11- public class MyScheduleReceiver extends BroadcastReceiver {
12-
13- // Restart service every 30 seconds
14- private static final long REPEAT_TIME = 1000 * 30 ;
15-
16- @ Override
17- public void onReceive (Context context , Intent intent ) {
18- AlarmManager service = (AlarmManager ) context
19- .getSystemService (Context .ALARM_SERVICE );
20- Intent i = new Intent (context , MyStartServiceReceiver .class );
21- PendingIntent pending = PendingIntent .getBroadcast (context , 0 , intent ,
22- PendingIntent .FLAG_CANCEL_CURRENT );
23- Calendar cal = Calendar .getInstance ();
24- // Start 30 seconds after boot completed
25- cal .add (Calendar .SECOND , 30 );
26- //
27- // Fetch every 30 seconds
28- // InexactRepeating allows Android to optimize the energy consumption
29- service .setInexactRepeating (AlarmManager .RTC_WAKEUP ,
30- cal .getTimeInMillis (), REPEAT_TIME , pending );
31-
32- // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
33- // REPEAT_TIME, pending);
34-
35- }
36- }
1+ package de .vogella .android .ownservice .local ;import java .util .Calendar ;import android .app .AlarmManager ;import android .app .PendingIntent ;import android .content .BroadcastReceiver ;import android .content .Context ;import android .content .Intent ;public class MyScheduleReceiver extends BroadcastReceiver { // Restart service every 30 seconds private static final long REPEAT_TIME = 1000 * 30; @Override public void onReceive(Context context, Intent intent) { AlarmManager service = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, MyStartServiceReceiver.class); PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); Calendar cal = Calendar.getInstance(); // Start 30 seconds after boot completed cal.add(Calendar.SECOND, 30); // // Fetch every 30 seconds // InexactRepeating allows Android to optimize the energy consumption service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending); // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), // REPEAT_TIME, pending); } }
0 commit comments