Cob50nm
Cob50nm

Reputation: 961

Switch status from android widget

I have an app with a widget that has a switch on it and a class that extends service to manage the content on the widget. I'm using RemoteViews to access the views to edit them but there are somethings that I want to do dependant on the sate of the switch. Is there a way to get the state of the switch? I've had a look through the api and couldn't find a way to do it.

Preemptively: I can't use findViewById because its not an activity

I can post code if needed.

For clarity the type of switch I want to use is one like this http://1.bp.blogspot.com/-AXi56wp5zVE/T6pAZj-MXdI/AAAAAAAAACM/-wT0w1PpcJ4/s1600/device-2012-05-09-152937.png

Upvotes: 0

Views: 609

Answers (1)

Karakuri
Karakuri

Reputation: 38595

Are you following the App Widgets Guide? I don't think the Switch widget is supported in RemoteViews.

Anyways, since you can't get any information on demand, you need to use setOnClickPendingIntent() and have the click send an intent to update your app widget. Stick an extra in the Intent specifying what state the button is in (or what state it would change to when clicked, either one). In the update logic, read the extra, change the UI, and set a new PendingIntent with the extra changed.

public class MyAppWidgetProvider extends AppWidgetProvider {
    public static final String ACTION_UPDATE_SWITCH = "MyAppWidgetProvider.UPDATE_SWITCH";
    public static final String EXTRA_SWITCH_ON = "MyAppWidgetProvider.EXTRA_SWITCH_ON";

    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (ACTION_UPDATE_SWITCH.equals(action)) {
            int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWDIGET_ID, 0);
            if (id != 0) {
                updateAppWidgetSwitch(context, intent, id);
            }
        }
        super.onReceive(context, intent);
    }

    private void updateAppWidgetSwitch(Context context, Intent intent, int appWidgetId) {
        boolean switchOn = intent.getBooleanExtra(EXTRA_SWITCH_ON, false);
        // take some action based on the switch being clicked

        RemoteViews views = new RemoteViews (context.getPackageName(), R.layout.app_widget_layout);
        // normal RemoteViews stuff
        // use switchOn var to set your switch state

        // make new on click pending intent
        Intent intent = new Intent(ACTION_UPDATE_SWITCH);
        intent.putExtra(AppWidgetManager.EXTRA_APPWDIGET_ID, id);
        intent.putExtra(EXTRA_SWITCH_ON, !switchOn); // new state
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.switch_id, pendingIntent);

        // update widget
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    // other AppWidgetProvider methods ...
}

In your manifest, add this intent filter to your AppWidgetProvider's <receiver> element. Note that if you change the value of the action string above, make sure you change it here as well.

<intent-filter>
    <action android:name="MyAppWidgetProvider.EXTRA_SWITCH_ON" />
</intent-filter>

Upvotes: 1

Related Questions