3

我有以下 AppWidgetProvider

public class MyWidgetProvider extends AppWidgetProvider {

    public final String TAG = "WidgetExample";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Log.w(TAG, "onUpdate method called");
        ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        Intent intent = new Intent("INIT_STATE");
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        context.sendBroadcast(intent);
    }
}

我有以下配置为使用该操作的广播接收器:

public class StateReceiver extends BroadcastReceiver {

    public final String TAG = "WidgetExample";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "StateReceiver got " + intent.getAction());

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);

        ComponentName smsComp = new ComponentName(context, SmsReceiver.class);
        PackageManager pm = context.getPackageManager();

        if ("INIT_STATE".equals(intent.getAction())) {
            // Lets check state..
            int currentState = pm.getComponentEnabledSetting(smsComp);
            if(currentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT || currentState == PackageManager.COMPONENT_ENABLED_STATE_DISABLED){
                Log.d(TAG, "State is " + currentState + ", matching views to be disabled");
                setViewsDisable(remoteViews);
            } else {
                Log.d(TAG, "State is " + currentState + ", matching views to be enabled");
                setViewsEnable(remoteViews);
            }
        } else if ("TOGGLE_STATE".equals(intent.getAction())) {
            int currentState = pm.getComponentEnabledSetting(smsComp);
            Log.d(TAG, "Current state is " + currentState);
            if (currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                disable(remoteViews, smsComp, pm);
            } else {
                enable(remoteViews, smsComp, pm);
            }
        }

        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

        for (int widgetId : allWidgetIds) {
            Log.d(TAG, "Setting the pending intents");
            Intent clickIntent = new Intent("TOGGLE_STATE");
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.backgroundImage, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }

    private void setViewsEnable(RemoteViews remoteViews) {
        remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.enabledshape);
        remoteViews.setTextViewText(R.id.txtStatus, "ENABLED");
    }

    private void setViewsDisable(RemoteViews remoteViews) {
        remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.disabledshape);
        remoteViews.setTextViewText(R.id.txtStatus, "DISABLED");
    }

    private void enable(RemoteViews remoteViews, ComponentName smsComp, PackageManager pm) {
        Log.d(TAG, "enabling");
         setViewsEnable(remoteViews);
        pm.setComponentEnabledSetting(smsComp, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

    private void disable(RemoteViews remoteViews, ComponentName smsComp, PackageManager pm) {
        Log.d(TAG, "disabling");
        setViewsDisable(remoteViews);
        pm.setComponentEnabledSetting(smsComp, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
}

我想要它做什么: 显示文本视图的简单小部件启用或禁用单击小部件应切换到其他状态切换状态后,它需要以编程方式启用或禁用接收器

当前发生的情况: AppWidgetProvider 中的 onUpdate 似乎是定期调用的(我认为是由 android 系统调用的),当这种情况发生时,它似乎会重新绘制小部件,就像它在创建时一样,完全无视在自创建小部件以来的远程视图。

删除视图是否异步更新?如何防止小部件恢复到其初始状态?只有当我切换到启用时才会出现问题,切换后它会翻转回禁用大约 1 秒钟。

4

0 回答 0