Home>

In addition to program applications for android development,There are also app widget applications. Many people develop application programs instead of app widget applications. This post is to help everyone learn how to develop app widget applications.

Let me briefly talk about the principle of app widgets. An app widget is a piece of information displayed on the desktop.Jump to the program entry class by clicking the app widget.And the program that comes with the system,The typical app widget is music, this android built-in music player applet.This is a typical app widget + app application. That is, a program can be launched either through the app widget or through the app. An app widget is an appwidgetprovider + a UI interface display (many intents are bound in advance), and the information on the interface can be changed through program control.Clicking on a widget on the widget can only fire an intent, or send a service startup notification.And appwidgetprovider can intercept this intent and perform corresponding processing (such as displaying new information).

The following simulates the application of the app widget

Launch the application in two ways

1.App widget launch

Long press on the blank desktop home screen will pop up "Add to Home Screen", then select "Widgets" option to enter "Select Widgets", and finally select the widget i want will be added to the desktop home screen,When you click on the desktop control you just added, you will enter the main entrance of the program.

2. App startup:same as ordinary activity

The following is the implementation code

main.xml layout file,Interface of the program entry class

my_layout.xml layout file:button with an image

<?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">
  <textview android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="program entry" />
</linearlayout>

Class mainactivity program entry class:

package com.ljq.activity;
import android.app.activity;
import android.os.bundle;
/**
 * Main program entry class
 *
 * @author jiqinlin
 *
 * /
public class mainactivity extends activity {
  @override
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
  }
}

The following code is the code used to develop appwidget:

<?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
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ + id/imageview"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />->
  <button android:id="@ + id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@ drawable/png1" />
</linearlayout>

my_appwidget.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<!-
appwidgetprovderinfo:Describes the size, update frequency, and initial interface of appwidget.
Exist as an xml file in the res/xml/directory of the application.
Note:After sdk1.5, this android:updateperiodmillis has no effect.
To create service updates yourself
 ->
<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minwidth="75dip"
  android:minheight="45dip"
  android:updateperiodmillis="1000"
  android:initiallayout="@ layout/my_layout" />

testactivity class:

package com.ljq.activity;
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;
/**
 * Add desktop controls to your phone,When you click the desktop control, you enter the main program
 *
 * appwidgetprovider:inherits from broadcastrecevier, and receives notifications when appwidget applies update, enable, disable, and delete.
 * Among them, onupdate, onreceive are the most commonly used methods,They receive update notifications
 *
 * @author jiqinlin
 *
 * /
public class testactivity extends appwidgetprovider {
  /**
   * Used to update app widgets at intervals. The interval time is defined by the updateperiodmillis property in appwidgetproviderinfo (unit:milliseconds).
   * Note:this android:updateperiodmillis will not work after SDK 1.5,To create service updates yourself.
   * This method is also called when the user adds an app widget.
So it should perform the basic setup,For example, define an event handler for the view and start a temporary service, if needed.
   * However, if you have already declared a configuration activity,This method will not be called when the user adds an app widget.
   * And only called in subsequent updates.
The configuration activity should be responsible for performing the first update when the configuration is complete.
   * /
  @override
  public void onupdate (context context, appwidgetmanager appwidgetmanager, int [] appwidgetids) {
    system.out.println ("onupdate");
    //Enter the main program entrance when clicking the desktop component
    intent intent=new intent (context, mainactivity.class);
    pendingintent pendingintent=pendingintent.getactivity (context, 0, intent, 0);
    //remoteviews class describes a view object can be displayed in other processes,Layout can be integrated with layout resource files.
    //Although this class is under android.widget.remoteviews instead of appwidget, it is often used in the development of android widgets,    //Mainly can be called across processes (appwidget is run uniformly by a service host).
    remoteviews myremoteviews=new remoteviews (context.getpackagename (), r.layout.my_layout);
    //myremoteviews.setimageviewresource(r.id.imageview, r.drawable.png1);//Set the properties of the layout control (pay special attention)
    myremoteviews.setonclickpendingintent (r.id.btn, pendingintent);
    componentname mycomponentname=new componentname (context, testactivity.class);
    //Responsible for managing appwidget and sending notifications to appwidgetprovider.
Provide updated appwidget status, get installed appwidget provided information and other related status
    appwidgetmanager myappwidgetmanager=appwidgetmanager.getinstance (context);
    myappwidgetmanager.updateappwidget (mycomponentname, myremoteviews);
  }
  /**
   * Called when the app widget is removed from the host.
   * /
  @override
  public void ondeleted (context context, int [] appwidgetids) {
    system.out.println ("ondeleted");
    super.ondeleted (context, appwidgetids);
  }
  /**
   * Called when an app widget instance is first created.
   * For example, if the user adds two instances of the app widget, it will only be called the first time.
   * If you need to open a new database or perform other settings that only need to happen once for all app widget instances,   * Then here is a good place to do the job.
   * /
  @override
  public void onenabled (context context) {
    system.out.println ("onenabled");
    super.onenabled (context);
  }
  /**
   * Called when the last instance of your app widget is removed from the host.
You should do some cleanup in onenabled (context),Such as deleting a temporary database
   * /
  @override
  public void ondisabled (context context) {
    system.out.println ("ondisabled");
    super.ondisabled (context);
  }
  /**
   * Will be called when each broadcast is received,And before the callback function above.
   * You usually don't need to implement this method,Because the default appwidgetprovider implements filtering all app widget broadcasts and calls the above method appropriately.
   * Note:There is a known issue in android 1.5,The ondeleted () method is not called when it is called.
   * To circumvent this problem,You can implement onreceive () as described in the group post to receive this ondeleted () callback.
   * /
  @override
  public void onreceive (context context, intent intent) {
    system.out.println ("onreceive");
    super.onreceive (context, intent);
  }
}

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ljq.activity" android:versioncode="1"
  android:versionname="1.0">
  <application android:icon="@ drawable/icon" android:label="@ string/app_name">
    <activity android:name=". Mainactivity" android:label="main program">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <!-The testactivity class is a broadcast receiver,Because testactivity inherits from appwidgetprovider->
    <receiver android:name=". Testactivity" android:label="Add Desktop Control">
      <intent-filter>
        <action android:name="android.appwidget.action.appwidget_update" />
      </intent-filter>
      <meta-data android:name="android.appwidget.provider"
        android:resource="@ xml/my_appwidget" />
    </receiver>
  </application>
  <uses-sdk android:minsdkversion="7" />
</manifest>
  • Previous ASP basic knowledge VBScript basic elements explained
  • Next Java multi-threaded implementation of asynchronous calling method