Simple to learn the steps to create Android widget desktop Widgets _android

Source: Internet
Author: User

First, the widget design steps
need to modify three XML, one class:

1. The first XML is the layout XML file (such as: Main.xml), which is the widget. In general, if you use this widget to display the time, then just declare a textview in this layout xml OK.

2. The second XML is Widget_provider.xml, which is primarily used to declare a appwidget. Among them, layout is the designation above that Main.xml.

3. The third XML is androidmanifest.xml, registering broadcastreceiver information.

4. The last class is used to do some business logic operations. Let it inherit the class Appwidgetprovider. There are many methods in Appwidgetprovider, in general we simply overwrite the OnUpdate (context,appwidgetmanager,int[]) method.

Second, the Code case

1. Define a widgetprovider to handle some callback of the widget
(1) onenable, called when the first widget is created.
(2) Ondisable, in contrast to Onenable, creates the last widget call.
(3) Called when an instance of Ondelete,widget is deleted.
(4) OnUpdate, called when the widget needs to update its view.
(5) OnReceive (): This method handles broadcastreceiver behavior by default and invokes the above method.

The public class Widgetdemoappwidgetprovider extends appwidgetprovider{ 
  the public void onUpdate Appwidgetmanager Appwidgetmanager, int[] appwidgetids) { 
    final int N = appwidgetids.length;    Perform this loop procedure for each App Widget, that belongs to this provider for     
     
    (int i=0; i<n; i++) {       
      int Appwidgetid = Appwidgetids[i];             
     
      Intent Intent = new Intent ();       
      Pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, 0);             
      Remoteviews views = new Remoteviews (Context.getpackagename (), r.layout.widget_demo_layout);       
      Views.setonclickpendingintent (R.id.wap_app, pendingintent);        
      Appwidgetmanager.updateappwidget (Appwidgetid, views);     
    }   
  } 
 

2. Register in Androidmanifast.xml provide

<receiver android:name= "Widgetdemoappwidgetprovider" > 
  <intent-filter> 
    <action android: Name= "Android.appwidget.action.APPWIDGET_UPDATE"/> 
  </intent-filter> 
  <meta-data android:name = "Android.appwidget.provider" 
    android:resource= "@xml/widget_demo_appwidget_info"/> 
</receiver > 

3. Create widget configuration XML under XML folder:

<?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android=

"http://schemas.android.com" /apk/res/android " 
  android:minwidth=" 60px " 
  android:minheight=" 60px " 
  android:initiallayout=" @ Layout/widget_demo_layout " 
  > 
</appwidget-provider> 

(4) Create widget layout

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:orientation=" vertical " 
  android:layout_width=" fill_parent " 
  android:layout_" height= "Fill_parent" 
  > 
  <imageview  
    android:id= "@+id/wap_app" android:layout_width= "WRAP_" 
    Content " 
    android:layout_height=" wrap_content " 
    android:src=" @drawable/icon "/> 
</ Linearlayout> 

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.