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.

有帮助吗?

解决方案

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.

其他提示

A service will be started only once. If you call startService() for a already started service there will not be any effect. Refer http://developer.android.com/guide/topics/fundamentals/services.html.

A service is "started" when an application component (such as an activity) starts it by
calling startService(). Once started, a service can run in the background indefinitely,
even if the component that started it is destroyed. 

When the service is not running, the intent should work properly.

Regarding

IntentService with the HTTPClient works fine and then when I switch over to Wifi the HTTPClient gets an UnknownHostException.

UnknownHostException occurs when there is no proper net connection. Check if the net connection is proper.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top