Switch status from android widget -


i have app widget has switch on , class extends service manage content on widget. i'm using remoteviews access views edit them there somethings want dependant on sate of switch. there way state of switch? i've had through api , couldn't find way it.

preemptively: can't use findviewbyid because not activity

i can post code if needed.

for clarity type of switch want use 1 http://1.bp.blogspot.com/-axi56wp5zve/t6pazj-mxdi/aaaaaaaaacm/-wt0w1ppcj4/s1600/device-2012-05-09-152937.png

are following app widgets guide? don't think switch widget supported in remoteviews.

anyways, since can't information on demand, need use setonclickpendingintent() , have click send intent update app widget. stick in intent specifying state button in (or state change when clicked, either one). in update logic, read extra, change ui, , set new pendingintent 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 action based on switch being clicked          remoteviews views = new remoteviews (context.getpackagename(), r.layout.app_widget_layout);         // normal remoteviews stuff         // use switchon var set 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 manifest, add intent filter appwidgetprovider's <receiver> element. note if change value of action string above, make sure change here well.

<intent-filter>     <action android:name="myappwidgetprovider.extra_switch_on" /> </intent-filter> 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -