android – 小部件上的方向更改按钮没有响应

我在窗口小部件上有两个按钮可以更改窗口小部件中的某些项目,如果在手机上更改了方向,按钮不起作用.我读了 http://developer.android.com/guide/topics/resources/runtime-changes.html,但这是关于活动而不是小部件.
@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,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,refresh,0);
    remoteViews.setonClickPendingIntent(R.id.button_refresh,refreshPendingIntent);

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

解决方法

我建议创建一个服务(可能在您的appwidgetprovider中进行子类化)并覆盖onConfigurationChanged()方法.使用该服务将允许您委派业务逻辑来处理该服务,构建和更新您的窗口小部件.它也将允许您管理轮换.如果您执行任何阻塞操作,那么该服务将是产生线程并将结果返回主UI线程以避免ANR的好地方.

我会建议如下:

public class MyWidget extends appwidgetprovider
{
    @Override
    public void onUpdate(Context context,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);
        }
    }
}

还有看看这个链接http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html

另外,请务必指出您有兴趣收到清单中的轮换更改通知.这样的事情将会起作用:
机器人:configChanges = “keyboardHidden |方向”
在清单内的服务声明中声明.

希望有帮助!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...