문제

문제가 생겼습니다.가끔씩 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();
        }
    }
}

인텐트 서비스 시작의 컨텍스트를 애플리케이션으로 설정해야 할 수도 있다고 생각했지만 솔직히 잘 모르겠습니다.

도움이 되었습니까?

해결책

문제를 알아 냈습니다 ...

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

다음과 같이 :

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();를 추가하면 모든 사용량이 끝난 후 닫혔는지 확인했습니다.지금 일하고 있습니다.

다른 팁

서비스는 한 번만 시작됩니다.이미 시작된 서비스에 대해 StartService ()를 호출하면 아무런 효과가 없습니다. http://developer.android.com/guide/topics/fundamensals/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 적절한 순 연결이 없을 때 발생합니다.순 연결이 적절한 지 확인하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top