Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager inside BroadcastReceiver when BOOT_COMPLETED

I have a Service "GroupsTaskAlarmChecker" that is called every 20 seconds by AlarmManager in onCreate of Groups.class activity this way:

int seconds = 20;

           Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class);
           pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0);

           AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

           Calendar calendar = Calendar.getInstance();
           calendar.setTimeInMillis(System.currentTimeMillis());
           calendar.add(Calendar.SECOND, 10);
           alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);

This works perfectly. But I need to do that when device boot. Then I know I have to make AndroidManifest like this:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver android:name=".ReceiverBoot">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED">
            <category android:name="android.intent.category.HOME">
        </category></action></intent-filter>
    </receiver>

and then mi broadcastReceiver like this:

 public class ReceiverBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int seconds = 20;

        Intent myIntent = new Intent(context, GroupsTaskAlarmChecker.class);
        pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);

    }
}

but inside this onReceive I dont know how can I do the same that I did before (with intent and alarmManager to start the service each 20 seconds). Error in this line:

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Is possible that I can't make an AlarmManager inside BroadcastReceiver?

I thank you all, I am an Android begginer and I need your help. Sorry for my english ;)

like image 225
carlosdiazp Avatar asked Mar 05 '13 10:03

carlosdiazp


1 Answers

To summarize the answers and comments above: the onReceive handler receives a context which can be used to access getSystemService and ALARM_SERVICE. Sample code:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Start periodic service.
        Calendar cal = Calendar.getInstance();
        Intent srvIntent = new Intent(context, MyService.class);
        PendingIntent pIntent = PendingIntent.getService(context, 0, srvIntent, 0);
        // Use context argument to access service
        AlarmManager alarm = 
                        (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        // Repeat every 5 seconds
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                                5000, pIntent);
        }
    }
}

Create a new class with this code and of course change MyReceiver and MyService to the names in your implementation.

like image 190
pmont Avatar answered Nov 05 '22 13:11

pmont