After orientation change buttons on a widget are n

2019-01-17 17:22发布

问题:

I have two buttons on a widget that change some items in a widget, if an orientation is changed on a phone, buttons do nothing. I read http://developer.android.com/guide/topics/resources/runtime-changes.html but this is all about activity not widget.

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

    Intent active = new Intent(context, TvWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    mDbHelper = new DbAdapter(context);
    fillChannelList(context, appWidgetIds[appWidgetIds.length-1]);
    Set<Integer> keys = channelsImages.keySet();
    Iterator<Integer> iter = keys.iterator();
    while(iter.hasNext())  
    {
        if(channelId == 0) 
        {
            channelId = iter.next();
            break;
        }
    }
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    Editor edit = settings.edit();
    edit.putInt("channelId", channelId);
    edit.putInt("appWidgetIds", appWidgetIds[appWidgetIds.length-1]);
    edit.commit();
    active.putExtra("net.aerosoftware.tvvodic.appWidgetIds", appWidgetIds);
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    remoteViews.setOnClickPendingIntent(R.id.button_next, actionPendingIntent);

    Intent refresh = new Intent(context, TvWidget.class);
    refresh.setAction(ACTION_WIDGET_REFRESH);
    refresh.putExtra("net.aerosoftware.tvvodic.appWidgetIds", appWidgetIds);
    PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refresh, 0);
    remoteViews.setOnClickPendingIntent(R.id.button_refresh, refreshPendingIntent);

    updateView(context);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

回答1:

I would suggest creating a Service (possibly subclassing this within your AppWidgetProvider) and overriding the onConfigurationChanged() method. Using the service will allow you to delegate your business logic to be handled by the service, build, and update your widget. It will also allow you to manage rotations. And if you're performing any blocking operations then the service would be a good place to spawn a Thread and return the result back to the main UI thread to avoid ANRs.

I would suggest something like the following:

public class MyWidget extends AppWidgetProvider
{
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        context.startService(new Intent(context, MyUpdateService.class));
    }

    public static class MyUpdateService extends Service
    {
        @Override
        public void onStart(Intent intent, int startId)
        {
            super.onStart(intent, startId);
            // Update the widget
            RemoteView remoteView = buildRemoteView(this);

            // Push update to homescreen
            pushUpdate(remoteView);

            // No more updates so stop the service and free resources
            stopSelf();
        }

        public RemoteViews buildRemoteView(Context context)
        {
            RemoteView updateView = null;

            updateView = new RemoteViews(context.getPackageName(), R.layout.my_widget_layout);
            // Your code to build and update the remote view


            return updateView;
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig)
        {
            int oldOrientation = this.getResources().getConfiguration().orientation;

            if(newConfig.orientation != oldOrientation)
            {
                // Update the widget
                RemoteView remoteView = buildRemoteView(this);

                // Push update to homescreen
                pushUpdate(remoteView);
            }
        }

        private void pushUpdate(RemoteView remoteView)
        {
            ComponentName myWidget = new ComponentName(this, MyWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(myWidget, updateViews);
        }
    }
}

Also have a look at this link: http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html

Also, be sure to indicate that you are interested in receiving notifications on rotation change within your manifest. Something like this will work: android:configChanges="keyboardHidden|orientation" declared within your service declaration inside the manifest.

Hope that helps!



回答2:

Whenever you update the look of your widget (using either an Activity or your Broadcast Receiver [App widget provider]), you must also reassign all the PendingIntents for the click handlers, and then call updateAppWidget() as normal.

Example with setTextViewText():

// This will update the Widget, but cause it to
// stop working after an orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}


// This is the correct way to update the Widget,
// so that it works after orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

Intent intent = new Intent(context, MyWidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, ...);
remoteViews.setOnClickPendingIntent(R.id.widget_click_button, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}


回答3:

The problem might be due to having two buttons on the Widget, which somehow causes problems. See here:

http://permalink.gmane.org/gmane.comp.handhelds.android.devel/98008

or

http://groups.google.com/group/android-developers/browse_thread/thread/578a4429c369c27c/273a53a96ddd10c5?lnk=gst&q=Widget+does+not+respond+when+phone+orientation+changes#273a53a96ddd10c5

But it's not yet clear what the true solution is (a solution that doesn't need to create a background Service).



回答4:

If you use

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

to change some state of a specific view inside the remote views object, you need to change the other views too, because the orientation change event will only remember the last RemoteViews object that you used to update your widget. It will recreate the widget with that last used RemoteViews object. That is why you always need to update all the views inside the RemoteViews object, and also all the listeners for your views.

The rest will be handled by the OS automatically.