1

我有问题。每隔一段时间,IntentServiceUpdateService02就会运行失败。我把日志条目放进去,这样我就可以调试了,这就是我得到的......

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

02-28 21:37:32.484:BroadcastReceiver - 主启动;设置闹钟

02-28 21:37:32.539: BroadcastReceiver - 收到警报服务

02-28 21:38:32.500: BroadcastReceiver - 收到警报服务

通常这应该发生:

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

02-28 21:37:32.484:BroadcastReceiver - 主启动;设置闹钟

02-28 21:37:32.539: BroadcastReceiver - 收到警报服务

02-28 21:38:32.500:更新服务——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 启动的上下文设置为应用程序,但老实说我不知道​​。

4

2 回答 2

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();确保我在每次使用后关闭它。现在效果很好。

于 2012-08-01T15:30:10.280 回答
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 在没有正确的网络连接时发生。检查网络连接是否正确。

于 2012-02-29T06:26:56.677 回答