1

問題が発生しました。時々、IntentServiceUpdateService02は実行に失敗します。デバッグできるようにログエントリを入力しました。これが取得したものです...

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

インテントサービスのスタートアップのコンテキストをアプリケーションに設定する必要があるのではないかと思いましたが、正直なところわかりません。

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 に答える