经过一个小部件的方向改变按钮没有响应部件、按钮、方向

2023-09-13 23:41:36 作者:赤脚踏古桥

我有一个小工具,改变一些项目在小部件,如果方向上的电话改变了两个按钮,按钮,什么也不做。我读http://developer.android.com/guide/topics/resources/runtime-changes.html但是这是所有关于活动不小部件。

  @覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)
{
    RemoteViews remoteViews =新RemoteViews(context.getPackageName(),R.layout.widget);

    意图激活=新的意图(背景下,TvWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    mDbHelper =新DbAdapter(上下文);
    fillChannelList(上下文,appWidgetIds [appWidgetIds.length-1]);
    设置<整数GT;键= channelsImages.keySet();
    迭代器<整数GT; ITER = keys.iterator();
    而(iter.hasNext())
    {
        如果(的channelID == 0)
        {
            的channelID = iter.next();
            打破;
        }
    }
    共享preferences设置= context.getShared preferences(preFS_NAME,0);
    编辑器编辑= 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(上下文,0,活性,0);
    remoteViews.setOnClickPendingIntent(R.id.button_next,actionPendingIntent);

    意图刷新=新的意图(背景下,TvWidget.class);
    refresh.setAction(ACTION_WIDGET_REFRESH);
    refresh.putExtra(net.aerosoftware.tvvodic.appWidgetIds,appWidgetIds);
    PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(上下文,0,刷新,0);
    remoteViews.setOnClickPendingIntent(R.id.button_refresh,refreshPendingIntent);

    的UpdateView(上下文);
    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
    super.onUpdate(背景下,appWidgetManager,appWidgetIds);
}
 

解决方案

我建议创建一个服务(可能继承这个你AppWidgetProvider内),并覆盖onConfigurationChanged()方法。使用该服务将允许您委派业务逻辑由服务来处理,构建和更新你的widget。它还允许你管理旋转。如果你正在执行任何阻塞操作,那么该服务会是一个很好的地方产生一个线程,并返回结果返回给主UI线程,以避免ANRS。

我会建议类似如下:

 公共类进myWidget扩展AppWidgetProvider
{
    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)
    {
        context.startService(新意图(背景下,MyUpdateService.class));
    }

    公共静态类MyUpdateService扩展服务
    {
        @覆盖
        公共无效ONSTART(意向意图,诠释startId)
        {
            super.onStart(意向,startId);
            //更新小部件
            远程视窗远程视窗= buildRemoteView(本);

            //推送更新到主屏幕
            pushUpdate(远程视窗)

            //没有更多的更新,所以停止服务和免费资源
            stopSelf();
        }

        公共RemoteViews buildRemoteView(上下文的背景下)
        {
            远程视窗的UpdateView = NULL;

            更新视图=新RemoteViews(context.getPackageName(),R.layout.my_widget_layout);
            //你的code建立和更新远程视图


            返回更新视图;
        }

        @覆盖
        公共无效onConfigurationChanged(配置NEWCONFIG)
        {
            INT oldOrientation = this.getResources()getConfiguration()方向。;

            如果(newConfig.orientation!= oldOrientation)
            {
                //更新小部件
                远程视窗远程视窗= buildRemoteView(本);

                //推送更新到主屏幕
                pushUpdate(远程视窗)
            }
        }

        私人无效pushUpdate(远程视窗远程视窗)
        {
            组件名进myWidget =新的组件名(本,MyWidget.class);
            AppWidgetManager经理= AppWidgetManager.getInstance(本);
            manager.updateAppWidget(进myWidget,updateViews);
        }
    }
}
 

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

另外,一定要表明你有兴趣在你的清单在接到旋转变化的通知。像这样将工作: 机器人:configChanges =keyboardHidden |定位 在清单内为您服务声明中表示。

希望帮助!

如何修改这个单片机程序,目的是按键切换,现在只有按键1能切换按键2没有反应

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

解决方案

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!