0

i am learning how to create a widget but i am stuck at this point. i have a widget that contains a button that when clicked, will launch an activity. but there are two activities that may launch and this depends on a Boolean value. the Boolean Value is true if the current Activity is MainActivity, and is false otherwise. but when clicking the button, it seems that the boolean value doesn't update to its new value here. am i doing something wrong. any help would be appreciaited.

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

Manifest

 <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>
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58

3 Answers3

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

Use SharedPreference. You may try to implement these two methods in your MainActivity. Set Lock method true or false to change your boolean variable. Then the next click in your widget open your second class.

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

Edit: You can update widget after you set the boolean variable in 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);

    }
Orcun Sevsay
  • 1,310
  • 1
  • 14
  • 44
  • Still didn't work, i am logging though the value and its changing in the activity, but i noticed that if i place a Log in the onUpdate method, then it will be called only one time at app launch and not everytime i click so maybe thats related – Rashad.Z Mar 31 '15 at 07:20
0

Try this i think it will useful to you::here

vijay
  • 1,475
  • 2
  • 16
  • 26
0

Found the solution using Programmatically update widget from activity/service/receiver

when i change the boolean value in the activity, i should call the following:

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 Method:

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);
        }
    }
Community
  • 1
  • 1
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58