Search code examples
androidalarmmanagerintentservice

IntentService fails to run in certain situations


I got a problem. Every once in a while, the IntentService UpdateService02 fails to run. I put log entries in so I could debug and here's what I get...

02-28 21:37:32.461: Main - Broadcast Sent

02-28 21:37:32.484: BroadcastReceiver - Main Started; Set Alarm

02-28 21:37:32.539: BroadcastReceiver - Received AlarmService

02-28 21:38:32.500: BroadcastReceiver - Received AlarmService

Usually this should happen:

02-28 21:37:32.461: Main - Broadcast Sent

02-28 21:37:32.484: BroadcastReceiver - Main Started; Set Alarm

02-28 21:37:32.539: BroadcastReceiver - Received AlarmService

02-28 21:38:32.500: UpdateService -- onHandleIntent()

Any ideas? Here's my Broadcast Receiver code...

Broadcast Receiver:

public class AlarmReceiver extends BroadcastReceiver {

    private static final int INTERVAL = 60*1000; // check every 60 seconds

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction() != null) {
            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Log.v(TAG, "BroadcastReceiver - Received Boot Completed; Set Alarm");
            
                setRecurringAlarm(context);
            }else if(intent.getAction().equalsIgnoreCase(Main.BROADCAST_STARTUP)){
                Log.v(TAG, "BroadcastReceiver - Main Started; Set Alarm");
            
                setRecurringAlarm(context);
            }else{
                Log.v(TAG, "BroadcastReceiver - Received " + intent.getAction());
            }
        }else{
            Log.v(TAG, "BroadcastReceiver - Received AlarmService");
        
            Intent i = new Intent(context, UpdateService02.class);
            context.startService(i);
        }
    }

    private void setRecurringAlarm(Context context) {
        Intent receiver = new Intent(context, AlarmReceiver.class);
        PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), INTERVAL, recurringDownload);
    }
}

Intent Service:

public class UpdateService02 extends IntentService {

    static DefaultHttpClient mClient = Client.getClient();
    private static final int LIST_UPDATE_NOTIFICATION = 100;

    public UpdateService02() {
        super("UpdateService02");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "UpdateService -- onHandleIntent()");
    
        try {
            HttpGet httpget = new HttpGet(url);
            HttpResponse response;
            response = mClient.execute(httpget);
            BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        
            Intent i = new Intent(BROADCAST_UPDATE);  
            i.putExtra("text", in.readLine().toString() + " updated");
            sendBroadcast(i);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I thought it might be I need to set the intentservice startup's context to be the applications, but I honestly have no idea.


Solution

  • I figured out the problem...

    I changed this:

    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        response = mClient.execute(httpget);
        BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
    
        Intent i = new Intent(BROADCAST_UPDATE);  
        i.putExtra("text", in.readLine().toString() + " updated");
        sendBroadcast(i);
    } catch (ClientProtocolException e) {
    

    to this:

    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        response = mClient.execute(httpget);
        BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
    
        Intent i = new Intent(BROADCAST_UPDATE);  
        i.putExtra("text", in.readLine().toString() + " updated");
        sendBroadcast(i);
        in.close();
    } catch (ClientProtocolException e) {
    

    Sometimes the reader would get "clogged" up and the next time it got called it would still be stuck trying to work on the last request. Adding in.close(); made sure I closed it after every usage. Works great now.