在某些情况下,IntentService无法运行

时间:2012-02-29 04:50:13

标签: android alarmmanager intentservice

我遇到了问题。每隔一段时间,IntentService UpdateService02就无法运行。我把日志条目放进去,所以我可以调试,这就是我得到的......

  

02-28 21:37:32.461:主要 - 发送广播

     

02-28 21:37:32.484:BroadcastReceiver - 主要开始;设置闹钟

     

02-28 21:37:32.539:BroadcastReceiver - 收到AlarmService

     

02-28 21:38:32.500:BroadcastReceiver - 收到AlarmService

通常会发生这种情况:

  

02-28 21:37:32.461:主要 - 发送广播

     

02-28 21:37:32.484:BroadcastReceiver - 主要开始;设置闹钟

     

02-28 21:37:32.539:BroadcastReceiver - 收到AlarmService

     

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

有什么想法吗?这是我的广播接收器代码......

广播接收器:

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);
    }
}

意图服务:

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();
        }
    }
}

我认为可能需要将intentservice启动的上下文设置为应用程序,但老实说我不知道​​。

2 个答案:

答案 0 :(得分:1)

我发现了问题......

我改变了这个:

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) {

到此:

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) {

有时候读者会被“堵塞”,下次被调用时,它仍然会被困在试图处理最后一个请求。添加in.close();确保我在每次使用后关闭它。现在效果很好。

答案 1 :(得分:0)

服务只会启动一次。如果为已启动的服务调用startService(),则不会产生任何影响。请参阅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. 

当服务未运行时,意图应该正常工作。

关于

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

当没有正确的网络连接时,会发生UnknownHostException。检查网络连接是否正确。