如何在执行窗口小部件操作之前检查值是否已更改

时间:2015-03-31 06:48:04

标签: android widget

我正在学习如何创建一个小部件,但我在这一点上陷入困​​境。我有一个小部件,其中包含一个按钮,单击该按钮将启动一个活动。但是有两个活动可能会启动,这取决于布尔值。如果当前Activity是MainActivity,则Boolean值为true,否则为false。但是当单击按钮时,似乎布尔值在此处不会更新为其新值。难道我做错了什么。任何帮助都会得到赞赏。

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

        final int N = appWidgetIds.length;

        for (int i = 0; i < N; i++)
        {
            final  int appWidgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.rz_widget_layout);

            Intent intent=null;


            if(MainActivity.WidgetAppLuancherChecker){intent= new Intent(context, MainActivity.class);}
            else{intent= new Intent(context, ActivityB.class);}

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.LaunchApp, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

清单

 <receiver android:name="com.AppWidget.ApWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/info_widget_layout" />
        </receiver>

3 个答案:

答案 0 :(得分:0)

public static boolean getLock(Context context) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean lock = prefs.getBoolean("lock", true);
            return lock;
        }

public static void setLock(Context context, boolean isMainActivity) {

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("lock", isMainActivity);
            editor.commit();
        }

使用SharedPreference。您可以尝试在MainActivity中实现这两种方法。设置Lock方法true或false以更改布尔变量。然后,您的小部件中的下一个单击打开您的第二个类。

if(MainActivity.getLock(context)){intent= new Intent(context, MainActivity.class);}
            else{intent= new Intent(context, ActivityB.class);}

编辑:您可以在SharedPreferences中设置布尔变量后更新窗口小部件。

 private void updateWidgetScreen(String updateData) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
        ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
        remoteViews.setTextViewText(R.id.currency, " Dollar");

        Intent intent = new Intent(this, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);

        Intent clickIntent = new Intent(this, MainActivity.class);
        PendingIntent clickPI = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);

    }

答案 1 :(得分:0)

试试这个我认为它对您有用:: here

答案 2 :(得分:0)

使用Programmatically update widget from activity/service/receiver

找到解决方案

当我更改活动中的布尔值时,我应该调用以下内容:

Intent intent = new Intent(activity,ApWidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        int ids[] = AppWidgetManager.getInstance(activity.getApplication()).getAppWidgetIds(new ComponentName(activity.getApplication(), ApWidgetProvider.class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
        activity.sendBroadcast(intent);

onUpdate方法:

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

        final int N = appWidgetIds.length;

        for (int i = 0; i < N; i++)
        {
            final  int appWidgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.rz_widget_layout);

            Intent intent=null;

            int j=0;
            if(MainActivity.WidgetAppLuancherChecker){j=1;intent= new Intent(context, MainActivity.class);}
            else{j=2;intent= new Intent(context, ActivityB.class);}

            PendingIntent pendingIntent = PendingIntent.getActivity(context,j,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.RelativeWidget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }