Simple use of Android App widgets

Source: Internet
Author: User
Tags home screen

The app widget is a widget for some desktops, such as the weather and some music playback apps, to the desktop;

For example:

The implementation steps and code are as follows:

(1) First, declare an app Widget in Androidmanifest.xml;

(1) Define Appwidgetproviderinfo object: Provide metadata for app widget, including layout, update frequency, etc., this object is defined in XML file;

Define a file named Example_appwidget_info.xml in the Res/xml folder;

(2) Specify the style and layout for the app widget:

Define a new layout file example_appwidget.xml;

(3) Realization Appwidgetprovider: Defines the basic life cycle function of the app widget;

OnUpdate: This method is called after the specified update time is reached or when the current user adds an app widget to the desktop;

Ondeleted: This method is called when the app widget is deleted;

Onenabled: This method is called when an instance of an app widget is created for the first time;

Ondisabled: When the last app widget instance is deleted, the method is called;

Onreveice: Receiving broadcast events;

Detailed implementation steps can be found in the official documentation, explained in great detail:

Http://android.toolib.net/guide/topics/appwidgets/index.html

Http://developers.androidcn.com/guide/topics/appwidgets/index.html#Providers

Code:

AndroidManifest.xml中声明 App Widget:

<receiverAndroid:name= "Exampleappwidgetprovider" >            <Intent-filter>                <ActionAndroid:name= "Android.appwidget.action.APPWIDGET_UPDATE" />            </Intent-filter>            <Meta-dataAndroid:name= "Android.appwidget.provider"Android:resource= "@xml/example_appwidget_info" />        </receiver>

Example_appwidget_info.xml in the Res/xml directory:

<xmlns:android= "http://schemas.android.com/apk/res/android"    Android : initiallayout= "@layout/example_appwidget"    android:minheight= "294DP"     android:minwidth= "72DP"    android:updateperiodmillis= "86400000"  ></appwidget-provider>

New Layout file Example_appwidget.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <TextViewAndroid:id= "@+id/widgettextid"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#00FFCC"Android:text= "Widget is a widget for some desktops" /></LinearLayout>

Define Class Exampleappwidgetprovider Inheritance Appwidgetprovider:

 PackageCom.example.appwidgettest2;ImportAndroid.appwidget.AppWidgetManager;ImportAndroid.appwidget.AppWidgetProvider;ImportAndroid.content.Context; Public classExampleappwidgetproviderextendsAppwidgetprovider {@Override Public voidOnUpdate (Context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) {System.out.println ("OnUpdate"); Super. OnUpdate (Context, Appwidgetmanager, appwidgetids); } @Override Public voidOndeleted (Context context,int[] appwidgetids) {System.out.println ("Ondeleted"); Super. ondeleted (context, appwidgetids); } @Override Public voidondisabled (Context context) {System.out.println ("Ondisabled"); Super. ondisabled (context); } @Override Public voidonenabled (Context context) {System.out.println ("Onenabled"); Super. onenabled (context); }}

To delete an app Widget:

Delete all:

On this basis, consider another example:

Add a button to the original app widget and click to jump to another activity;

The implementation is as follows:

Use Pendingintent and remoteviews to achieve;

(1) There are 3 ways to create pendingintent;

 

int int   int int int. INT INT flags)

(2) The role of Remoteviews:

The Remoteviews object represents a series of view objects;

The object represented by the Remoteviews is running in another thread;

(3) because the app widget and our application are running in different processes (the view in the app widget is running in the home screen process), it is not possible to bind the listener in the same way as before;

Instead, the remoteviews.setonclickpendingintent (r.id.widgetbuttonid,pendingintent) should be used to achieve it;

Targetactivity.java

 Package Com.example.appwidgettest2; Import android.app.Activity; Import Android.os.Bundle;  Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (r.layout.target_activity);    }}

Target_activity.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:textsize= "20SP"android:gravity= "Center"Android:text= "Jump to this activity" /></LinearLayout>

and Exampleappwidgetprovider modified to:

 PackageCom.example.appwidgettest2;Importandroid.app.PendingIntent;ImportAndroid.appwidget.AppWidgetManager;ImportAndroid.appwidget.AppWidgetProvider;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.widget.RemoteViews; Public classExampleappwidgetproviderextendsAppwidgetprovider {@Override Public voidOnUpdate (Context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) {System.out.println ("OnUpdate");  for(inti = 0; i < appwidgetids.length; i++) {System.out.println (appwidgetids[i]); //Create a Intent objectIntent Intent =NewIntent (context, targetactivity.class); //Create a pendingintentPendingintent pendingintent = pendingintent.getactivity (context, 0, Intent,0); Remoteviews remoteviews=Newremoteviews (Context.getpackagename (), r.layout.example_appwidget); //binds the event handler to the button,//The first parameter is used to specify the ID of the bound processor control; //The second parameter is used to specify which pendingintent will be specified when the event occurs.remoteviews.setonclickpendingintent (R.id.widgetbuttonid, pendingintent); //Update AppwidgetAppwidgetmanager.updateappwidget (Appwidgetids[i], remoteviews); }        Super. OnUpdate (Context, Appwidgetmanager, appwidgetids); } @Override Public voidOndeleted (Context context,int[] appwidgetids) {        Super. ondeleted (context, appwidgetids); } @Override Public voidondisabled (Context context) {Super. ondisabled (context); } @Override Public voidonenabled (Context context) {Super. onenabled (context); }}

Simple use of Android App widgets

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.