An introduction to Android development appwidget usage Analysis _android

Source: Internet
Author: User
Tags sqlite database stub

This example describes the Android Appwidget usage. Share to everyone for your reference, specific as follows:

App Widgets is a view of a small application that can be embedded in other applications (such as a desktop program) and can be refreshed periodically.

Before you create the app widget, you need to understand the following concepts

Appwidgetproviderinfo objects

It is a description of the app Widget metadata, such as appwidget layout, refresh frequency, and Appwidgetprovider classes that are defined in XML.

Implementation of Appwidgetprovider Class

The app widget defines some basic methods for the (callback interface), which are based on the broadcast event (broadcast events), through which you can receive broadcasts when the app widget is updated, created, invalid, uninstalled.

View layout

Define the initial layout for the app widget and use XML to lay out

Alternatively, you can implement a configured activity for the app widget, an optional activity that allows the user to modify or configure the app widget when they add your app widget ready for creation.

Here we start creating an app Widget

① the app Widget in manifest

First, declare the Appwidgetprovider in the Androidmanifest.xml

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

<receiver> element requires Android:name attribute, Appwidgetprovider as reference
<intent-filter> element Be sure to include the <action> element and then the action will have the Android:name attribute, which needs to be specified to receive action_appwidget_update The broadcast of the Appwidgetprovider, that is Exampleappwidgetprovider, the broadcast must be expressly stated, This is important because Appwidgetmanager automatically sends broadcasts of all other applications to Appwidgetprovider.

<meta-data> element specifies that the Appwidgetproviderinfo resource requires the following properties
Android:name-Specifies the name of the metadata, and the Android.appwidget.provider represents the data as the Appwidgetproviderinfo description.
Android:resource-the location of the reference Appwidgetproviderinfo resource
In a word: Appwidgetprovider and Appwidgetproviderinfo in the Androidmanifest.xml main declaration concept

② Add appwidgetproviderinfo meta data

Appwidgetproviderinfo defines some of the most basic data for appwidgeet, such as the minimum size of the layout, the initialization of the layout resources, how to update the app widget, and the configurable activity when creating the app widget (optional)

To define a Appwidgetproviderinfo object can be defined with an XML resource file, saved in res/xml/own file name, XML file with a single element <appwidget-provider> see the following example:

<appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android"
  android:minwidth= "294DP"
  android:minheight= "72DP"
  android:updateperiodmillis= "86400000"
  android:previewimage= "@drawable Preview "
  android:initiallayout=" @layout/example_appwidget "
  android:configure=" Com.example.android.ExampleAppWidgetConfigure "
  android:resizemode=" horizontal|vertical ">
</ Appwidget-provider>

Initiallayout: Specify the App widget layout resource file
Configure: An activity that configures attributes for an app widget when it is created
Updateperiodmillis:appwidget Update Frequency

③ Create app Widget layout

You have to define an initialization layout file for your app widget, and you can put the layout file in the res/layout/directory. You can design your app widget with the following list
View object, but before you start, familiarize yourself with the app Widget design guidelines.

If you are familiar with XML Layouts, it will be easy to create an app widget, however, you must realize that the app widget layout is based on Remoteviews, and it's not supporting each layout and view widget.

A RemoteView object can support only the following layout layout classes:
Framelayout LinearLayout Relativelayout

The following widget classes are supported:
AnalogClock Button chronometer ImageButton imageview progressbar TextView viewflipper ListView GridView
StackView Adapterviewflipper

PS: The inherited classes of these classes are also unsupported.

Here are the Appwidget in the demo I wrote this time.

The function is very simple for preliminary understanding of Appwidget, Response to Appwidget button events

Package com.manymorere.appwidget;
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 ACTION = "COM.MANYMORE.APPW
  Idget ";
  private int id;
    @Override public void ondeleted (context context, int[] appwidgetids) {System.out.println ("ondeleted");
  Super.ondeleted (context, appwidgetids); @Override public void ondisabled (context context) {//TODO auto-generated Method Stub System.out.println ("O
    Ndisabled ");
  super.ondisabled (context); @Override public void onenabled (context context) {//TODO auto-generated Method Stub System.out.println (' On
    Enabled ");
  super.onenabled (context); @Override public void OnReceive (context context, Intent Intent) {System.out.println ("onreceive"); Its own defined ACTION if (intent.getaction (). Equals (action)) {remoteviews RemoteView = new Remoteviews (CONTEXT.GETP
      Ackagename (), r.layout.appwidget_layout);
      Remoteview.setcharsequence (R.id.button, "SetText", "22222222222222");
      Appwidgetmanager Appwidgetmanager = appwidgetmanager.getinstance (context);
      ComponentName componentname = new ComponentName (context, exampleappwidgetprovider.class);
      Appwidgetmanager.updateappwidget (ComponentName, RemoteView);
    SYSTEM.OUT.PRINTLN ("Receive custom Action");
    }else{super.onreceive (context, intent);
    } @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {
    int N = Appwidgetids.length;
      for (int i = 0; i<n; i++) {int appwidgetid = appwidgetids[i];
      Intent Intent = new Intent (ACTION);
      Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0); RemoteviEWS remoteviews = new Remoteviews (Context.getpackagename (), r.layout.appwidget_layout);
      Remoteviews.setonclickpendingintent (R.id.button, pendingintent);
      Appwidgetmanager.updateappwidget (Appwidgetid, remoteviews);
    System.out.println (Appwidgetid);
  } super.onupdate (Context, Appwidgetmanager, appwidgetids);

 }
}

Each time you add a Appwidget instance, the OnUpdate () method is invoked and the button onclick event is registered, and when you click on it, a broadcast is sent, and the broadcast is defined by itself. Then the OnReceive method receives the broadcast and then reacts again, and I am here to modify the text on the TextView.

Because Appwidget and its applications, although in a program, but run, they are not in a process, so in the call method and modify the interface is somewhat limited, there is no ordinary (activity on the view of some operations) so free, So want to do some operation on the view of the Appwidget, or update appwidget, generally need to use the Remoteviews,appwidgetmanager and may also use the ComponentName

Personal understanding: Remoteviews at the time of creation represents a collection of view in a Appwidget instance, while ComponentName represents an entire Appwidget instance

Full Instance code click here Download the site .

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.