2012-02-29 3 views
1

문제가 있습니다. 가끔 IntentService UpdateService02이 실행되지 않습니다. 37 : 나는 ...IntentService가 특정 상황에서 실행되지 않습니다.

02-28 21 내가 디버깅 할 수 있도록에서 로그 항목을 넣고 여기에 내가 무엇을 얻을 32.461가 : - : 37 : 32.484 : 홈페이지 브로드 캐스트

02-28 (21)를 보낸 브로드 캐스트 리시버를 - 시작된 메인; 알람 설정

02-28 21 : 37 : 32.539 : 브로드 캐스트 리시버 - AlarmService

02-28 21 수신 : 38 : 32.500을 : 브로드 캐스트 리시버 - AlarmService

수신은

보통 이런 일이 발생한다 :

02-28 21 : 37 : 32.461이 : - : 37 : 32.484 : 홈페이지 브로드 캐스트

02-28 (21)를 보낸 브로드 캐스트 리시버 - 주요 시작; 알람 설정

02-28 21 : 37 : 32.539 : 브로드 캐스트 리시버 -

02-28 21 AlarmService

수신 : 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); 
    } 
} 

의도 서비스 : 여기에 ...

방송 수신기 내 방송 수신기 코드의 나는 그것이 내가 응용 프로그램이기 위하여 intentservice 시작의 컨텍스트를 설정해야 될 줄 알았는데

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

, 그러나 나는 정직하게 모른다.

+0

어디에서 UpdateService - onHandleIntent() 로그를 넣었습니까? onStartCommand() 서비스에서? – user936414

+0

내 IntentService를 보여주기 위해 편집 해 줄 수 있다고 생각했습니다. – Rawr

+0

IntentService가 실행되지 않을 때 서비스가 이미 실행되고 있는지 확인 했습니까? – user936414

답변

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); 
    in.close(); 
} catch (ClientProtocolException e) { 

때때로 독자 및 다음 시간까지 "막힌"얻을 것입니다 마지막 요청을 처리하려고 계속 붙어있을 것입니다. in.close();을 추가 할 때마다 매번 사용을 마친 후에 닫혔습니다. 이제 잘됩니다.

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의 관해서는

일어난다. 인터넷 연결이 올바른지 확인하십시오. 이것에

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

: 내가 문제를 파악

+0

두 가지 ... 1) IntentService는 특별한 서비스 사례입니다. 모든 요청이 처리 될 때 요청을 대기열에 넣고 중지합니다. startService를 여러 번 호출하고 서비스가 이미 실행 중이면 onStart 메서드가 매번 호출되고 onCreate는 아직 존재하지 않으면 한 번만 호출합니다. 나는 IntentService가 작업을 마친 후에도 끝났다고 확신합니다. 심지어 onHandle이 이미 실행 중이 더라도 호출 될 것입니다. 그렇지 않습니까? – Rawr

+0

2) UnknownHostException을 조사한 결과 인터넷 연결이 정상적으로 작동하지만 원인은 무엇입니까? 다른 모든 응용 프로그램은 계속해서 인터넷에 계속 액세스합니다. 그리고이 문제의 유일한 다른 원인 인 내 매니페스트에 인터넷 사용 권한이 있습니다. 나는 심지어 인터넷에 접속하기 위해 와이파이 연결을 사용하는 데 필요한 허가가 있는지 조사하는 시간을 보냈다. – Rawr

+0

URL 문자열에 http : //가 있습니까? – user936414

관련 문제