配置更改后小部件不刷新

Posted

技术标签:

【中文标题】配置更改后小部件不刷新【英文标题】:widget doesn't refresh after configuration changes 【发布时间】:2011-12-07 16:23:01 【问题描述】:

我为我的应用编写了一个 AppWidget,你可以选择一个你喜欢的地方的名称,它会显示来自那个地方的图片。

它有一个配置活动(我使用了与应用本身相同的配置活动) 首次添加时,小部件和它的配置处理工作正常:我选择了一个我喜欢的地方,我看到了图片;

问题是每当我重新启动设备(或者它退出睡眠模式) 然后当我点击它(小部件)时,我进入配置活动 - 更改为所需的图片,没有任何反应 当我调试时,我可以看到:

appWidgetManager.updateAppWidget(mAppWidgetId, views);
mAppWidgetId = 

有一个id,而views 不是null。 那么黑客是怎么回事? 我的想法是,在设备重新启动时,widgetId 会发生变化,并且我不会像我应该的那样处理它。 顺便说一句,如果在模拟器上尝试相同,我没有问题一切正常

这是我的代码:

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.dryrun" android:versionCode="3"
    android:versionName="1.1" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/ic_launcher_test"
        android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:debuggable="true"><!-- different< android:theme="@style/Theme.NoBackground" -->

        <!-- Main Activity -->
        <activity android:name=".MyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Preferences -->

        <activity android:name=".Preferences.EditPreferences">
             <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </activity>

        <!-- Widgets -->

        <!--  Widget-->
        <receiver android:name=".Widget.testWidget" android:label="@string/app_widget_">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <!--action
                    android:name="com.test.dryrun.Widget.testWidget.PREFENCES_WIDGET_CONFIGURE" /-->
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/test_widget__provider" />
        </receiver>
        <service android:name=".Widget.testWidget$WidgetService" />

        <uses-permission android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
    </application>
</manifest>

appwidget_provider xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
 android:minWidth="146dip"
 android:minHeight="146dip"
 android:updatePeriodMillis="0"
 android:initialLayout="@layout/test_widget"
/>

小部件类

public class testWidget extends AppWidgetProvider 
    public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
        Intent svcIntent = new Intent(context, WidgetService.class);
        widgets = appWidgetIds;
        context.startService(svcIntent);
    


  @Override
  public void onReceive(Context context, Intent intent) 
  

        // v1.5 fix that doesn't call onDelete Action
        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
        
            final int appWidgetId = intent.getExtras().getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
            
                this.onDeleted(context, new int[]  appWidgetId );
            
        
        else
        
            super.onReceive(context, intent);           
        
    

    //public void updateWidget()
    /**
    * @param context
    * @param remoteViews
    */
    public static void updateWidget(Context context, RemoteViews remoteViews)
    
        String Prefix = context.getString(R.string._prefix);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String ToShow = prefs.getString(context.getString(
                     R.string.Widget_string),
                     context.getString(R.string.default_string));

        String pkgName = context.getPackageName();
        int resID = context.getResources().getIdentifier(Prefix + ToShow, "drawable", pkgName);

        WidgetController widgetController = WidgetController.getInstance();
        widgetController.setRemoteViewImageViewSource(remoteViews, R.id.WidgetImage, resID);
    

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

            // Push update to homescreen
            WidgetController.getInstance().pushUpdate(
                    remoteView, 
                    getApplicationContext(),
                    testWidget.class);

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

        public RemoteViews buildRemoteView(Context context)
        
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_widget);

            Intent configIntent = new Intent(context, EditPreferences.class);
            configIntent.setAction(testWidget.PREFENCES_WIDGET_CONFIGURE);


            configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgets[0]);

            //configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.WidgetImage, runtestPendingIntent);

            WidgetController controller =  WidgetController.getInstance();

            controller.updateWidget(context, remoteViews);

            return remoteViews;
        

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

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

                // Push update to homescreen
                WidgetController.getInstance().pushUpdate(
                        remoteView, 
                        getApplicationContext(),
                        testWidget.class);
            
        

        @Override
        public IBinder onBind(Intent arg0)
        
            // TODO Auto-generated method stub
            return null;
        
    

偏好类

public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener

    @Override
    public void onCreate(Bundle savedInstanceState) 
    
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Intent intent = getIntent();
        m_extras = intent.getExtras();
    
    private Bundle m_extras;

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    
        if(key.equals(getString(R.string.rlvntString)))
        
            Context ctx = getApplicationContext();
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
            setResult(RESULT_CANCELED);
            if (m_extras != null) 
            
                Intent resultValue = new Intent();

                String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID;
                mAppWidgetId = m_extras.getInt(
                            stringID, 
                            AppWidgetManager.INVALID_APPWIDGET_ID);
                RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.test_widget);

                WidgetController.getInstance().updateWidget(ctx, views);

                appWidgetManager.updateAppWidget(mAppWidgetId, views);
                resultValue.putExtra(stringID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            
        
    

【问题讨论】:

【参考方案1】:

buildRemoteView 必须在 EditPreferences 中定义,而不是在 Widget 类中。我遇到了同样的问题,但在研究了这个之后:http://developer.android.com/guide/topics/appwidgets/index.html#Configuring 我将所有 remoteView 建筑物移动到配置活动 - 它现在正在工作

【讨论】:

以上是关于配置更改后小部件不刷新的主要内容,如果未能解决你的问题,请参考以下文章

HomeScreen 重新启动后小部件冻结

设备重启后小部件消失

应用更新后小部件消失了

php 后小部件后的创世纪。

SwiftUI 小部件无法刷新

更新小部件列表视图