微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 |方向”
在清单内的服务声明中声明.

希望有帮助!

原文地址:https://www.jb51.cc/android/311404.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐