The add widget requires some processing before this can be done using the configure Activity
Based on the implementation of the previous article, plus the configuration activity (configure=activity). When you join the widget. will open an activity, configure the operation,
<appwidget-provider....android:configure= "Com.stone.ui.AppWidgetConfigureActivity" ></ Appwidget-provider>
To configure the implementation of an activity:
Package Com.stone.ui;import Android.app.activity;import Android.appwidget.appwidgethost;import Android.appwidget.appwidgetmanager;import Android.content.intent;import Android.os.bundle;import Android.view.view;import android.view.view.onclicklistener;import Android.widget.button;import com.stone.R;/* * Config part * Add Setresult (result_cancle) before Setcontentview () function in OnCreate, so that if you press the Back button before the activity is initialized, the widget will not start; After the Setcontentview () function (not necessarily in oncreate. Before activity exits), add the following settings, for example, to specify the widget */public class Appwidgetconfigureactivity extends activity implements that need to be started Onclicklistener {Button config1, config2, config3; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Intent Intent = Getintent (); Bundle extras = Intent.getextras (); if (extras! = null) {int widgetid = Extras.getint (appwidgetmanager.extra_appwidget_id ,-1);//derived from Intent widgetid//notification appwdiget configuration canceled Intent Reslut = new Intent (); Reslut.putextra (Appwidgetmanager.extra_ appwidget_id, Widgetid); SetresulT (result_canceled, Reslut); SYSTEM.OUT.PRINTLN ("result Cancel");} Setcontentview (r.layout.switch_configure); config1 = (Button) Findviewbyid (R.ID.BTN_CONFIG1); config2 = (Button) Findviewbyid (R.ID.BTN_CONFIG2); config3 = (Button) Findviewbyid (R.ID.BTN_CONFIG3); Config1.setonclicklistener (This) ; Config2.setonclicklistener (this); Config3.setonclicklistener (this);} @Overrideprotected void Onresume () {Super.onresume ();} private void Oncompletedconfigure () {Intent Intent = getintent (); Bundle extras = Intent.getextras (); if (extras! = null) {int widgetid = Extras.getint (appwidgetmanager.extra_appwidget_id ,-1);//from Intent widgetid//notification appwdiget configuration is complete Intent reslut = new Intent (); Reslut.putextra (Appwidgetmanager.extra_ appwidget_id, Widgetid); Setresult (RESULT_OK, Reslut); SYSTEM.OUT.PRINTLN ("result ok"); finish (); System.out.println ("Finish OK");}} @Overridepublic void OnClick (View v) {if (v = = config1) {//do config1} else if (v = = config2) {//do Config2} else if (v = = CONFIG3) {//do config3}oncompletedconfiGure ();}}
Switch_configure.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:id= "@+id/btn_config1" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" config1 "/><button android:id=" @+ Id/btn_config2 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " Android : text= "Config2"/><button android:id= "@+id/btn_config3" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Config3"/></linearlayout>
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android widget Gadget (two) using configure