2010-08-09 6 views
6

onUpdate() 메서드에서 일부 배열을 초기화 한 후 인 텐트와 버튼을 사용하여 onUpdate() 메서드에서 설정된 배열에 액세스 할 수없는 onReceive() 함수를 호출하려고합니다. 왜 그런가요? 이러한 배열은 객체 변수이며 public으로 선언됩니다. 내가 빠진 것이 있습니까?위젯 OnUpdate, onReceive

onReady() : 다음의 AppWidgetProvider의 API에서

package net.aerosoftware.widgettest; 

import java.util.HashMap; 
import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.RemoteViews; 

public class WidgetTest extends AppWidgetProvider { 

    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; 
    public HashMap<Integer, String> channelsImages; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     Log.e("UPDATE", "Start"); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); 

     channelsImages = new HashMap<Integer, String>(); 
     channelsImages.put(0, "one"); 
     channelsImages.put(1, "two"); 

     Intent active = new Intent(context, WidgetTest.class); 
     active.setAction(ACTION_WIDGET_RECEIVER);  
     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
     remoteViews.setOnClickPendingIntent(R.id.buttonclick, actionPendingIntent); 

     super.onUpdate(context, appWidgetManager, appWidgetIds); 
     appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
     Log.e("UPDATE", "End"); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Log.e("RECEIVE", "Start 2"); 
     if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
     { 
      try 
      { 
       Log.e("SIZE", "Size Of channel array: "+channelsImages.size()); 
      } 
      catch(Exception e) 
      { 
       Log.e("ON_RECIEVE_ERROR", " "+e.getMessage()); 
      } 
     } 
     super.onReceive(context, intent); 
     Log.e("RECEIVE", "End"); 
    } 

} 
+2

"액세스 할 수 없음"이란 무엇을 의미합니까? 어떤 종류의 예외가 있습니까? 내용이 비어 있습니까? – DuduAlul

+0

예, 널 포인터 예외입니다. – dfilkovi

+0

질문에 코드를 추가하면 쉽게 작업 할 수 있습니다. – DuduAlul

답변

4

API : "A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active."

이를 피하기 위해 서비스를 사용할 수 있습니다.

+0

위젯에서 "onUpdate"를 실행하고 싶습니다. 내 위젯에 새로 고침 버튼이 있습니다. AppWidgetProvider에서 그렇게 할 수 있습니까? 아니면 pendingclickevent를 사용해야합니까? 감사. – Si8

0

. "이것은 모든 방송을 요구하고 위의 각각의 콜백 메쏘드 이전 당신이 일반적으로이 방법으로 인해 구현 할 필요가 없습니다 기본 AppWidgetProvider 구현은 모든 App 위젯 브로드 캐스트를 필터링하고 적절하게 위 메소드를 호출합니다. " 당신이 널 점점 이유) onReceive()가의 onUpdate (전에 호출받을 것을 의미

은, 그건

당신의 AppWidgetProvider (가 상속하기 위해 (때문에) 브로드 캐스트 리시버)의 다른 인스턴스

을 받고

http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html

+0

아니요, 나는 데이터를 설정 한 후 수신기를 사용합니다. if (intent.getAction(). equals (ACTION_WIDGET_RECEIVER)) – dfilkovi

+0

코드의 짧은 부분을 추가하면 더 많은 도움이 될 것입니다. – DuduAlul

+0

소스 코드를 추가했습니다. – dfilkovi

관련 문제