Simple Example of android widget desktop plug-in

Source: Internet
Author: User

To create a plug-in that can be displayed on the desktop, three steps are required: 1. inherit AppWidgetProvider 2. compile the interface xml of the widget and write the mywidget property xml 3. androidManifest. the xml registration plug-in is a self-incrementing number, which is very simple. 1. inherit from AppWidgetProvider [java] public class MyWidgetProvider extends AppWidgetProvider {private static Timer myTimer; private static int index = 0; private final String broadCastString = "knowheart. wd. appWidgetUpdate "; @ Override public void onDeleted (Context context, int [] appWidgetIds) {// TODO Auto-generated method stub super. onDeleted (context, appWidgetIds);} @ Override public void onEnabled (Co Ntext context) {// TODO Auto-generated method stub super. onEnabled (context) ;}@ Override public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {// TODO Auto-generated method stub super. onUpdate (context, appWidgetManager, appWidgetIds) ;}@ Override public void onReceive (Context context, Intent intent) {super. onReceive (context, intent) ;}first, we add a class to inherit the Ap PWidgetProvider, which is used to control the update and deletion operations of our plug-in. Here we will use three methods: onEnabled, onUpdate, and onReceive. compile the widget interface xml and write the mywidget attributes xmllayout/widget_layout.xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "#000000" android: layout_gravity = "center_horizontal" android: layout_width = "300dp" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/update" android: text = "0" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center"/> </Lin EarLayout> Create a folder named xml and add an xml file named xml/mywidget. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android" android: minWidth = "300dip" android: minHeight = "72dp" android: updatePeriodMillis = "86400000" android: initialLayout = "@ layout/widget_layout"> </appwidget-provider> set the length, width, and refresh time of the plug-in. 0 indicates manual refresh, and xml of the plug-in interface. 3. androidManifest. xml registration plug-in [html] <! -- Register the plug-in --> <javaser android: name = "MyWidgetProvider"> <intent-filter> <action android: name = "android. appwidget. action. APPWIDGET_UPDATE "/> <action android: name =" com. wd. appWidgetUpdate "> </action> </intent-filter> <meta-data android: name =" android. appwidget. provider "android: resource =" @ xml/mywidget "/> </javaser> <action android: name =" com. wd. appWidgetUpdate "> This section registers a broadcast to the system. We will send the broadcast to the AppW during logic processing. Update method of idgetProvider to achieve manual refresh. After completing the above three steps, a plug-in is displayed on the desktop. If we want the number displayed by the plug-in to add one per second, we need to perform background processing [java] public class MyWidgetProvider extends AppWidgetProvider {private static Timer myTimer; private static int index = 0; // define the event private final String broadCastString = "com. wd. appWidgetUpdate "; @ Override public void onDeleted (Context context, int [] appWidgetIds) {// TODO Auto-generated method stub super. onDeleted (context, appWidgetIds); System. out. println ("onDeleted") ;}@ Override public void onEnabled (Context context) {System. out. println ("onEnabled"); // TODO Auto-generated method stub super. onEnabled (context); // This is called when the plug-in is created. Therefore, we enable a timer to execute MyTask mMyTask = new MyTask (context) every second ); myTimer = new Timer (); myTimer. schedule (mMyTask, 1000,100 0); System. out. println ("onEnabled2") ;}@ Override public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {System. out. println ("onUpdate"); // TODO Auto-generated method stub super. onUpdate (context, appWidgetManager, appWidgetIds) ;}@ Override public void onReceive (Context context, Intent intent) {// when it is determined that the event was sent, we will get the plug-in interface, and then import the index self-added to the textview System. out. println ("onReceive"); if (intent. getAction (). equals (broadCastString) {index ++; RemoteViews rv = new RemoteViews (context. getPackageName (), R. layout. widget_layout); rv. setTextViewText (R. id. update, Integer. toString (index); // display the interface to AppWidgetManager appWidgetManager = AppWidgetManager in the plug-in. getInstance (context); ComponentName componentName = new ComponentName (context, MyWidgetProvider. class); appWidgetManager. updateAppWidget (componentName, rv);} // TODO Auto-generated method stub super. onReceive (context, intent);} class MyTask extends TimerTask {private Context mcontext = null; private Intent intent = null; public MyTask (Context context) {// create a new Intent mcontext = context; intent = new Intent (); intent. setAction (broadCastString) ;}@ Override public void run () {System. out. println ("2"); // send broadcast (received by onReceive) mcontext. sendBroadcast (intent );}}}

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.