1

I'm currently having a problem integrating AlarmManager and BroadcastReceiver.

Im my app, I'm running a background service that runs regardless that app is running or not. I think I get this part working fine. The background service keeps an array that changes based on user's location. However, at 8:00am everyday, I want the app to reset the array variable to default. After much looking around online, it seems like the way to do this is via AlarmManager (to initiate the task every 8:00am) and using BroadcastReceiver (to receive the alarm, and perform the task).

So basically the code goes something like this:

public class BackgroundService extends Service {

    private ArrayList thisArray;

    private BroadcastReceiver thisReceiver = new BroadcastReceiver(){

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

    }

    @Override
    public void onCreate(){

        super.onCreate();
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.MINUTE, 2); //example

        this.registerReceiver(thisReceiver, new IntentFilter("BackgroundService"));

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ONE_MINUTE, "what to put here?"); //example, repeat every minute

    }

    private void setArrayToDefault(){
        //here, the array will be changed back to default values
    }
}

My main issue is on how to set the AlarmManager to call thisReceiver everytime it's set. Any idea? Is my approach correct?

ImpStudent
  • 123
  • 7
  • 15

1 Answers1

1

Im my app, I'm running a background service that runs regardless that app is running or not.

Please don't. This is why users attack us with task killers and the Force Stop from the Manage Services screen in Settings.

My main issue is on how to set the AlarmManager to call thisReceiver everytime it's set. Any idea?

You are not registering the BroadcastReceiver, so AlarmManager will not be able to contact it.

Please please please please please please please redesign your service such that it does not have to run all of the time. Android will be killing off your service due to old age, anyway, so if you want a reliable and stable app, you need to do this redesign, anyway.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So how do you think I should approach this? Basically part of the app is that it will get user's location (probably every 30 mins, regardless if the user is currently running the app or not). If the user is currently in one of the location as specified in the Array and notification is enabled, users will be sent a notification (and no more notification can be send for the rest of the day). The next day, all notifications will be re-enabled (thus the need of AlarmManager). – ImpStudent Aug 27 '11 at 18:27
  • 1
    @ImpStudent: "(probably every 30 mins, regardless if the user is currently running the app or not)" -- use `AlarmManager` and my `LocationPoller` so you do not need to keep a service in memory all of the time -- https://github.com/commonsguy/cwac-locpoll -- everything else you cite should be maintained in a database or other persistent store. – CommonsWare Aug 27 '11 at 18:38
  • okay I did more reading and it seems like it is actually possible to do my intended plan without the use of a background service. Thanks for your guide. – ImpStudent Aug 29 '11 at 02:08
  • I've tried using LocationPoller but somehow, it never managed to get my location. Im currently using Emulator. I've followed your demos and guide correctly but it still cannot detect my location. Any idea whats wrong? – ImpStudent Aug 29 '11 at 04:32
  • You need to feed a location to the emulator via DDMS or telnet, while `LocationPoller` is looking for a fix (i.e., has an active `requestLocationUpdates()`. Personally, I have only tried `LocationPoller` on hardware, so there might be some emulator-specific issue that I am not aware of. – CommonsWare Aug 29 '11 at 05:59