Android Widget Desktop Component Development Introduction _android

Source: Internet
Author: User

Android Widget Desktop Component development

Widget is one of the features introduced by the Android1.5 version. Widget, which allows users to understand the important information displayed in the program in a timely manner. The standard Android system already contains examples of several widgets, such as analog clocks, music players, etc.

I. Appwidget Framework class

1,Appwidgetprovider : Inherits from Broadcastrecevier, receives the notification when Appwidget applies update, enable, disable and delete. Among them, OnUpdate, OnReceive is the most commonly used method, they receive update notification.

2, appwidgetprovderinfo: Describes the size of appwidget, update frequency and the initial interface and other information, in the form of XML file in the application of the res/xml/directory.

3,Appwidgetmanger : Responsible for the management of Appwidget, send notifications to Appwidgetprovider.

4,remoteviews : A class that can be run in other application processes, send notifications to Appwidgetprovider.

II. Introduction to the main categories of the Appwidget framework

1) Appwidgetmanger class

Bindappwidgetid (int appwidgetid, componentname provider)
Binding Appwidgetid with a given componentname

Getappwidgetids (ComponentName provider)
Get Appwidgetid through a given componentname

Getappwidgetinfo (int appwidgetid)
Obtaining Appwidget information through Appwidgetid

Getinstalledproviders ()
Returns a list<appwidgetproviderinfo> of information

getinstance (context)
Gets the context object used by the Appwidgetmanger instance

Updateappwidget (int[] appwidgetids, remoteviews views)
Modify the incoming RemoteView by Appwidgetid and refresh the Appwidget component again

Updateappwidget (componentname provider, remoteviews views)
Modify the incoming Remoeteview by componentname and refresh the Appwidget component again

Updateappwidget (int appwidgetid, remoteviews views)
Modify the incoming RemoteView by Appwidgetid and refresh the Appwidget component again

2 The following methods can be implemented by inheriting from Appwidgetprovider :

1, ondeleted (context, int[] appwidgetids)

2, ondisabled (context)

3, onenabled (context)

4, OnReceive (context, Intent Intent)
Tip: Because Appwidgetprovider is inherited from Broadcastreceiver so you can rewrite the Onrecevie method, of course, you must register in the background receiver

5, OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids)

Three, Demo detailed

1. To create the widget content provider file, we set up an XML folder under RES and create a new Widget_provider.xml code:

<?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android=   
"http://schemas.android.com" /apk/res/android "  
  android:minwidth=" 50dip "  
  android:minheight=" 50dip "  
  android:updateperiodmillis=" 10000 "  
  
  android:initiallayout=" @layout/main "  
/>   

Tip: Above said Appwidgetprovderinfo is in Res/xml file form exists, see parameter is not difficult to understand, more important is here android:initiallayout= "@layout/main" This sentence is the layout file for the specified desktop component.

The main parameters are set as follows:

MinWidth: defines the width of the Wdiget component

MinHeight: defines the height of the Wdiget component

Updateperiodmillis: updated time period

Layout files for initiallayout:widget

Configure: If you need to start an activity to set up before you start, give the full class name of the activity here (which, as you'll say, is slightly different from the general activity implementation)

The *widget size is computed:(cell number *74) -2,api is said to prevent integer rounding in pixel calculations to cause error so-2 ... I don't understand.

2. Modify the Main.xml layout, the code is as follows:

<?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"  
  android:background= "@drawable/wordcup"  
  >   
<textview android:id=    
  "@+id /wordcup "  
  android:layout_width=" fill_parent "   
  android:layout_height=" wrap_content "   
  android:text=" @string/hello "  
  android:textsize=" 12px "  
  android:textcolor=" #ff0000 "  
  />
</linearlayout >

Tips: An XML file that defines the layout of the Widget interface (located in res/layout/.), it should be noted that the components used must be supported by Remoteviews, and the components currently supported in the native API are as follows:

Framelayout, LinearLayout, Relativelayout

AnalogClock, Button, Chronmeter, ImageButton, ImageView, ProgressBar, TextView

* If other components are used, the widget is created to cause android.view.InflateExceptionn exceptions.

PS: This leads to some features or styles that cannot be implemented, such as a very basic list or text edit box that cannot be implemented directly. If you want to customize view in the widget, you can only provide support for the appropriate component by modifying the framework.

3. Write a class to inherit from Appwidgetprovider

Package com.android.tutor;   
Import Java.util.Calendar;   
Import Java.util.Date;   
Import Java.util.GregorianCalendar;   
Import Java.util.Timer;   
Import Java.util.TimerTask;   
Import Android.appwidget.AppWidgetManager;   
Import Android.appwidget.AppWidgetProvider;   
Import Android.content.ComponentName;   
Import Android.content.Context;   
Import Android.widget.RemoteViews; The public class Widetdemo extends Appwidgetprovider {/** called ' when the ' activity is ' is a. */@Overri   
       
    De public void onUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {   
    Timer timer = new timer ();   
    Timer.scheduleatfixedrate (New MyTime (Context,appwidgetmanager), 1, 60000);   
  Super.onupdate (context, Appwidgetmanager, appwidgetids);   
    Private class MyTime extends timertask{remoteviews remoteviews;   
    Appwidgetmanager Appwidgetmanager;   
       
    ComponentName Thiswidget; PuBlic mytime (Context Context,appwidgetmanager appwidgetmanager) {this.appwidgetmanager = Appwidgetmanager;   
         
      Remoteviews = new Remoteviews (Context.getpackagename (), r.layout.main);   
    Thiswidget = new ComponentName (context,widetdemo.class);   
      public void Run () {Date date = new Date ();   
      Calendar calendar = new GregorianCalendar (2010,06,11);   
      Long days = ((Calendar.gettimeinmillis ()-date.gettime ())/1000)/86400;   
      Remoteviews.settextviewtext (R.id.wordcup, "South Africa World Cup also" + days+ "Days");   
         
    Appwidgetmanager.updateappwidget (Thiswidget, remoteviews);   }   
       
  }   
    
}

Appwidgetprovider is actually a broadcastreceiver, which provides the following functions:

OnReceive (context, Intent)

OnUpdate (context, Appwidgetmanager, int[] appwidgetids)

Onenabled (context)

Ondeleted (context, int[] appwidgetids)

Ondisabled (context)

The above function can be rewritten to monitor the change of widget state and handle it accordingly.

OnUpdate is called when a component is built on the desktop and updates the component Ui,onreceiver call Update UI for receiving broadcasts, which is generally more common.

Widget updates are different from activity and must be aided by Remoteviews and Appwidgetmananger.

function call Cycle

[Start-no confiure activity] 
onreceive
onenabled--First widget is displayed
onreceive
onupdate--Refresh Interface

[Start-with confiuration activity] 
onreceive
onUpdate

[drag] 
< no state change >

[cycle Update] 
OnReceive
onUpdate

[delete] 
onreceive
ondeleted--widget deleted
onreceive
The last widget was removed

[not enough at startup] 
onreceive
onenabled
onreceive
onUpdate
onreceive
ondeleted
onreceive
ondisabled

* Each state of the change will trigger the onreceive, the general function is not required to rewrite.

Iv. Modify the configuration file Androidmanifest.xml, the background registration receiver, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=   
"http://schemas.android.com/apk/res/" Android "  
   package=" Com.android.tutor "  
   android:versioncode=" 1 "  
   android:versionname=" 1.0 ">   
  <application android:icon= "@drawable/icon" android:label= "@string/app_name" >   
    <receiver android:name= ". Widetdemo "  
         android:label=" @string/app_name ">   
      <intent-filter>   
        <action android:name=" Android.appwidget.action.APPWIDGET_UPDATE "/>   
      </intent-filter>   
      <meta-data android:name=" Android.appwidget.provider "  
            android:resource=" @xml/widget_provider "  
      />   
    </receiver>   
  </application>   
  <uses-sdk android:minsdkversion= "7"/>   
</manifest>   

Tips:

Because it is a desktop component, the activity interface is not considered for the time being, and of course you may need to jump to the action application when you implement the project, typically a music player provided by Android.

The above code is more important is this sentence <meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/appwidget_ Provider "></meta-data> to the Appwidgetprovderinfo file for the specified desktop application so that it can be used as its administrative file.

V. Add Modify Resources

Add picture footage.

<?xml version= "1.0" encoding= "Utf-8"?>   
<resources>   
  <string name= "Hello" >hello world, widetdemo!</string>   
  <string name= "app_name" >DaysToWorldCup</string>   
</resources >

These are the details of the Android widget, hoping to help friends in need.

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.