2011-05-16 2 views
1

이 문제의 배경. 서버에 http 게시물을 보내고 서버에서받은 데이터로 위젯을 업데이트하는 업데이트 서비스를 사용하여 주기적으로 설정된 간격으로 업데이트하는 앱과 연결된 appwidget이 있습니다.App 위젯 업데이트 서비스 강제 종료 - 도움이 필요합니다!

필자가 테스트 및 사용자 보고서에서 발견 한 사실은 강제 종료의 특정 인스턴스가 위젯이 업데이트되고 네트워크 상태가 사용 가능에서 사용 불가로 변경되면 주기적으로 발생한다는 것입니다. NYC 지하철에서 내 전화로이 사실을 알았 기 때문에.

디버깅하는 동안 응답이 수신되기 전에 http 포스트가 발생하고 네트워크 상태가 변경되면 기본적으로 IOException이 수신되는 것으로 나타났습니다. 그래서이 예외를 처리하고이 특별한 경우 위젯을 기본 업데이트로 업데이트했습니다. 괜찮 았어.

하지만 흥미롭게도이 강제 종료를 다시 눈치 채고이를 해결하는 방법이 부족합니다.

이전에이 문제가 발생했는데이를 어떻게 처리 할 수 ​​있는지 알고 있습니까?

java.lang.NullPointerException 
    at android.widget.RemoteViews$ReflectionAction.writeToParcel(RemoteViews.java:399) 
    at android.widget.RemoteViews.writeToParcel(RemoteViews.java:1003) 
    at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetProvider(IAppWidgetService.java:402) 
    at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:283) 
    at com.tyu.android.TyuWidget$UpdateService$1.run(TyuWidget.java:167) 

모든 당신이 전문가가 문제를보다 잘 이해하는 데 도움이 및 초보자 도움이 될 몇 가지 코드 조각.

@Override 
public void onReceive(Context context, Intent intent) { 
     check_intent = intent.getAction(); 
      if(check_intent.equals("android.appwidget.action.APPWIDGET_UPDATE")){ 
     this.onUpdate(context, intent); 
     } 
} 

다음은 OnUpdate 메소드 코드 스 니펫입니다.

public void onUpdate(Context context, Intent intent){ 
      Intent widgetUpdate = new Intent(context, TyuWidget.class); 
      widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
      AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
      PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate,PendingIntent.FLAG_UPDATE_CURRENT); 
      alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+ PERIOD, newPending); 
      context.startService(new Intent(context, UpdateService.class)); 

} 

위젯을 업데이트하는 UpdateService 클래스의 OnStart 메소드 내부에 스레드가 있습니다.

widgetUpdateThread = new Thread(){ 
       @Override 
       public void run(){ 
        RemoteViews updateViews = buildUpdate(getApplicationContext()); 
        if(updateViews!=null){ 
         ComponentName thisWidget = new ComponentName(getApplicationContext(), TyuWidget.class); 
         AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext()); 
         manager.updateAppWidget(thisWidget, updateViews); 
        } 
        else{ 
         updateViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.tuwidget); 
         updateViews.setImageViewResource(R.id.ad, R.drawable.tyu_null_game); 
         Intent defineIntent1 = new Intent(getApplicationContext(), Tyu3.class); 
         PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 
           0 /* no requestCode */, defineIntent1, 0 /* no flags */); 
         updateViews.setOnClickPendingIntent(R.id.tuwidget, pendingIntent1); 
         ComponentName thisWidget = new ComponentName(getApplicationContext(), TyuWidget.class); 
         AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext()); 
         manager.updateAppWidget(thisWidget, updateViews); 

        } 
       } 
      };    
      widgetUpdateThread.start(); 

buildUpdate 메소드 코드 스 니펫.

public RemoteViews buildUpdate(Context context) { 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
response = httpclient.execute(httppost); 
entity = response.getEntity(); 
is = entity.getContent(); 

//etc etc...and then I return the update view and it gets updated. 
} 

도움 주셔서 감사합니다.

+0

NPE는 어떤 라인에서 유래 되었습니까? –

+0

@Programmer Bruce manager.updateAppWidget (thisWidget, updateViews); inside widgetUpdateThread run() ... 안드로이드 마켓 강제 종료 보고서에서 말하는 내용. com.tyu.android.TyuWidget $ UpdateService $ 1.run (TyuWidget.java:167) – Aakash

+0

오류가 발생했을 때 해당 코드 행에 아무 것도 없습니까? –

답변

2

Service 클래스를 확장 한 UpdateService 클래스의 OnStart() 메서드 안에있는 스레드가 문제를 만들고있었습니다.

솔루션 :

가 widgetUpdateThread 스레드를 제거하고 대신 IntentService 사용하는 서비스 클래스를 확장하는 당함. 이것은 자동으로 스레드를 생성하고 매력처럼 작동합니다. OnStart 대신 IntentService를 사용할 때 onHandleIntent와 Voila의 코드를 @Override 안에 넣습니다! FruitNinja와 같이 매우 CPU와 메모리를 많이 사용하는 앱을 병렬로 사용할 때도 강제 종료되지 않습니다.

얘들 아! IntentService는 개발자 커뮤니티와 Android Framework 엔지니어가 적극 권장합니다. 위젯을 업데이트하는 동안 확인하십시오.

0

thisWidget 값이 manager.updateAppWidget (thisWidget, updateViews)에서 null 일 수 있습니다.

희망이 도움이됩니다. CD.