Android-Desktop component AppWidget

Source: Internet
Author: User

AppWidget is usually called a desktop plug-in. It is an important part of the Android Application Development layer. It is a method to embed a process control into a window of a process other than a process, it is a component program that interacts with the client program.

Let's take a look:

The following example records how to create an AppWidget and how it interacts with the client program:

Step 1: Create an xml folder under the res directory and create <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + eXd4X2FwcHdpZGdldF9pbmZvLnhtbDwvcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;">
Step 2: Create the layout file ywx_appwidget.xml

 
         
             
       
        
 

Step 3: Create ExampleAppWidgetProvider. java to implement the method in AppWidgetProvider

Package com. ywx. appwidget_04; import android. app. pendingIntent; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. componentName; import android. content. context; import android. content. intent; import android. widget. remoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider {private static final String UPDATE_ACTION = "com. ywx. appwidget_04.UPDATE_APP_WIDGET "; // receives broadcast events @ Overridepublic void onReceive (Context context, Intent intent) {super. onReceive (context, intent);} // this method is called after the specified Update time is reached or when the user adds an App Widget to the desktop @ Overridepublic void onUpdate (Context context, appWidgetManager appWidgetManager, int [] appWidgetIds) {super. onUpdate (context, appWidgetManager, appWidgetIds);} // this method is called when an AppWidget is deleted @ Overridepublic void onDeleted (Context context, int [] appWidgetIds) {super. onDeleted (context, appWidgetIds);} // this method is called when an AppWidget instance is created for the first time @ Overridepublic void onEnabled (Context context) {super. onEnabled (context);} // call this method after the last AppWidget instance is deleted @ Overridepublic void onDisabled (Context context) {super. onDisabled (context );}}


Step 4: declare in AndroidManifest. xml: There are two actiong actions, one is System and the other is custom. An event is triggered if any action is satisfied.

             
                              
              
                              
              
          
 

After completing the above four steps, you can complete the simple AppWidget example.
PendingIntent and RemoteViews are mainly used in AppWidget development. The usage record is as follows:
PendingIntent has three main methods:
1. getActivity (Context context, int requestCode, Intent intent, int flags)
2. getBroadcast (Context context, int requestCode, Intent intent, int flags)
3. getService (Context context, int requestCode, Intent intent, int flags)
RemoteViews: indicates that a series of view objects and the indicated objects run in another process.
The appwidget component program is not in the current process, so the listener binding event is different from the original one:
Add in onUpdate () method

Intent intent = new Intent (); intent. setAction (UPDATE_ACTION); // use getBroadcast to generate PendingIntent. When the object is executed, send broadcast PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. ywx_appwidget); remoteViews. setOnClickPendingIntent (R. id. widgetBut, pendingIntent); appWidgetManager. updateAppWidget (appWidgetIds, remoteViews );

Add
String action = intent. getAction (); if (UPDATE_ACTION.equals (action) {RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. ywx_appwidget); remoteViews. setImageViewResource (R. id. imageId, R. drawable. upload); remoteViews. setTextViewText (R. id. widgetText, "Wahaha"); AppWidgetManager appWidgeManager = AppWidgetManager. getInstance (context); ComponentName componentName = new ComponentName (context, ExampleAppWidgetProvider. class); appWidgeManager. updateAppWidget (componentName, remoteViews);} else {super. onReceive (context, intent );}


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.