Widget onUpdate, onReceive

大城市里の小女人 提交于 2019-12-10 04:35:56

问题


I initialize some arrays in an onUpdate() method and after that, using an intent and a button, I try to call onReceive() function which runs fine but cannot access arrays set in onUpdate() method. Why is that? Those array's are object variables and are declared public. Am I missing something?

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

}

回答1:


You are getting a different instance of AppWidgetProvider (since it extends BroadcastReceiver)

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."

You can use a service in order to avoid that.




回答2:


From the AppWidgetProvider API:

onReady(): "This is called for every broadcast and before each of the above callback methods. You normally don't need to implement this method because the default AppWidgetProvider implementation filters all App Widget broadcasts and calls the above methods as appropriate."

which means that onReceive() get invoked before onUpdate(), that's why you are getting null

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



来源:https://stackoverflow.com/questions/3438565/widget-onupdate-onreceive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!