Android Widget Desktop component creation

Source: Internet
Author: User
Package com. king. android. speical; import com. king. android. r; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. componentName; import android. content. context; import android. content. pm. packageManager; import android. widget. remoteViews;/*** Description: Create widget class * Author: Andy. liu * Time: 07:01:39 **/public class ExampleAppWidgetProvider extends AppWidgetProvider {@ Overridepublic void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {final int N = appWidgetIds. length; for (int I = 0; I <N; I ++) {int appWidgetId = appWidgetIds [I]; String titlePrefix = WidgetActivity. loadTitlePref (context, appWidgetId); updateAppWidget (context, appWidgetManager, appWidgetId, titlePrefix);} super. onUpdate (context, appWidgetManager, appWidgetIds);} // call @ Overridepublic void onDeleted (Context context, int [] appWidgetIds) when the Desktop component is deleted) {// Delete appWidgetfinal int N = appWidgetIds. length; for (int I = 0; I <N; I ++) {WidgetActivity. deleteTitlePref (context, appWidgetIds [I]) ;}}// call @ Overridepublic void onEnabled (Context context) {PackageManager pm = context when the first component provided by AppWidtgetProvider is deleted. getPackageManager (); pm. setComponentEnabledSetting (new ComponentName ("com. king. android. special ",". exampleBroadCastReceiver "), PackageManager. COMPONENT_ENABLED_STATE_ENABLED, PackageManager. DONT_KILL_APP);} // call @ Overridepublic void onDisabled (Context context) {PackageManager pm = context when the last component provided by AppWidtgetProvider is deleted. getPackageManager (); pm. setComponentEnabledSetting (new ComponentName ("com. king. android. special ",". exampleBroadCastReceiver "), PackageManager. COMPONENT_ENABLED_STATE_ENABLED, PackageManager. DONT_KILL_APP);} // update static void updateAppWidget (Context context, AppWidgetManager appwidgetManager, int appWidgetId, String titlePrefix) {// construct the RemoteViews object to update the Desktop component RemoteViews views = new RemoteViews (context. getPackageName (), R. layout. appwidget_provider); // update the text content, specifying the layout component views. setTextViewText (R. id. appwidget_text, titlePrefix); // update RemoteViews to AppWidget to update appwidgetManager. updateAppWidget (appWidgetId, views );}}

 

 

Package com. king. android. speical; import java. util. arrayList; import android. appwidget. appWidgetManager; import android. content. broadcastReceiver; import android. content. context; import android. content. intent;/*** Description: information received by creating a broadcast class * Author: Andy. liu * Time: 07:03:30 **/public class extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// update AppWidgetString action = intent through BroadCastReceiver. getAction (); if (action. equals (Intent. ACTION_TIMEZONE_CHANGED) | action. equals (Intent. ACTION_TIME_CHANGED) {AppWidgetManager gm = AppWidgetManager. getInstance (context); ArrayList <Integer> appWidgetIds = new ArrayList <Integer> (); ArrayList <String> texts = new ArrayList <String> (); WidgetActivity. loadAllTitlePrefs (context, appWidgetIds, texts); // update all AppWidgetfinal int N = appWidgetIds. size (); for (int I = 0; I <N; I ++) {ExampleAppWidgetProvider. updateAppWidget (context, gm, appWidgetIds. get (I), texts. get (I ));}}}}

 

Package com. king. android. speical;

Import java. util. arrayList; import android. app. activity; import android. appwidget. appWidgetManager; import android. content. context; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; import android. view. view; import android. widget. editText; import com. king. android. r; public class WidgetActivity extends Activity {private static final StringPREFS_NAME = "com. king. a Ndroid. exampleAppWidgetProvider "; private static final StringPREF_PREFIX_KEY =" prefix _ "; intmAppWidgetId = AppWidgetManager. INVALID_APPWIDGET_ID; EditTextmAppWidgetPrefix; public WidgetActivity () {super ();} public void onCreate (Bundle icicle) {super. onCreate (icicle); setResult (RESULT_CANCELED); setContentView (R. layout. appwidget_configure); mAppWidgetPrefix = (EditText) findViewById (R. id. appwidget_prefix); fin DViewById (R. id. save_button). setOnClickListener (mOnClickListener); Intent intent = getIntent (); Bundle extras = intent. getExtras (); if (extras! = Null) {mAppWidgetId = extras. getInt (AppWidgetManager. EXTRA_APPWIDGET_ID, AppWidgetManager. INVALID_APPWIDGET_ID);} if (mAppWidgetId = AppWidgetManager. INVALID_APPWIDGET_ID) {finish ();} mAppWidgetPrefix. setText (loadTitlePref (WidgetActivity. this, mAppWidgetId);} View. onClickListenermOnClickListener = new View. onClickListener () {public void onClick (View v) {final Context context = WidgetActivity. this; Stri Ng titlePrefix = mAppWidgetPrefix. getText (). toString (); saveTitlePref (context, mAppWidgetId, titlePrefix); // obtain the AppWidgetManager instance AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (context); // update AppWidgetExampleAppWidgetProvider. updateAppWidget (context, appWidgetManager, mAppWidgetId, titlePrefix); Intent resultValue = new Intent (); resultValue. putExtra (AppWidgetManager. EXTRA_APPWIDGET_ID, MAppWidgetId); setResult (RESULT_ OK, resultValue); finish () ;}}; static void saveTitlePref (Context context, int appWidgetId, String text) {SharedPreferences. editor prefs = context. getSharedPreferences (PREFS_NAME, 0 ). edit (); prefs. putString (PREF_PREFIX_KEY + appWidgetId, text); prefs. commit ();} static String loadTitlePref (Context context, int appWidgetId) {SharedPreferences prefs = context. getSharedPreferen Ces (PREFS_NAME, 0); String prefix = prefs. getString (PREF_PREFIX_KEY + appWidgetId, null); if (prefix! = Null) {return prefix;} else {return "xxxxxxxxxxxxxxxxx" ;}} static void deleteTitlePref (Context context, int appWidgetId) {} static void loadAllTitlePrefs (Context context, arrayList <Integer> appWidgetIds, ArrayList <String> texts ){}}

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> <EditText android: id = "@ + id/appwidget_prefix" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/save_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ android: string/OK"/> </LinearLayout>

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android" android: minWidth = "100dp" android: minHeight = "50dp" android: updatePeriodMillis = "86400000" android: initialLayout = "@ layout/appwidget_provider"/>

<? Xml version = "1.0" encoding = "UTF-8"?>

<TextView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/appwidget_text" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.