Android Desktop Component App widget usage Tutorial _android

Source: Internet
Author: User
Tags home screen

This example describes the Android Desktop Component App widget usage. Share to everyone for your reference. Specifically as follows:

Android apps, as well as apps, are apps for app widgets. Many people will develop application apps without developing app widget apps. This post is to help you learn how to develop app widget applications.

Let's briefly talk about the principle of the app widget. The app widget is something that displays information on the desktop, and jumps to the program entry class by clicking the app widget. And the system's own program, the typical app widget is music, this android built in the video player applet. This is a typical app Widget+app application. Is that a program can be launched either through the app widget or through the app. The App widget is a appwidgetprovider+ UI display (a lot of intent), and the information on the interface can be changed by program control, and clicking on the widget can only trigger a intent to be sent. Or issue a service launch notification. and Appwidgetprovider can intercept the intent and do the appropriate processing (such as displaying new information).

Here's a simulation of app widget applications

Launching an application in two different ways

1, App widget launch

A long blank desktop home screen pops up "Add to main screen", then select the widget option to go to the Select widget, and then select the widget you want to add to the desktop Home screen, and then click on the desktop control you just added to the program's main entrance.

2, App startup: As with ordinary activity

Following is the implementation code

Main.xml layout file, interface of program entry class

My_layout.xml layout file: A button with a picture

<?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 of the appwidget, update frequency and the initial interface and other information, exists in the res/xml/directory of the application in the form of an XML file.
Note: This android:updateperiodmillis is invalidated after SDK1.5 to create a service update
 -->
<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; /** * To add desktop controls to the mobile phone, when clicked on the desktop control to enter the main program * Appwidgetprovider: Inherit from the Broadcastrecevier, in the Appwidget application update, enable,
 Receive notifications when disable and delete. * Among them, onUpdate, OnReceive is the most commonly used method, they receive update notification * * @author Jiqinlin * */public class Testactivity extends Appwidgetprovide
   R {/** * is used to update the app Widget at intervals, defined in milliseconds by the Updateperiodmillis attribute in Appwidgetproviderinfo.
   * Note: This android:updateperiodmillis is invalidated after SDK1.5 to create a service update on its own.
   * This method will also be invoked when the user adds the app widget, so it should perform the underlying settings, such as defining an event handler for the view and starting a temporary services service, if needed. * However, if you have declared a configuration activity, this method will not be invoked when the user adds the app widget, * and is invoked only on 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 ");
    Click on the desktop component to enter the main program entrance Intent intent=new Intent (context, mainactivity.class);
    Pendingintent pendingintent=pendingintent.getactivity (context, 0, intent, 0);
    The Remoteviews class describes a view object that can be displayed in other processes and can integrate layout resource files to achieve layout.
    Although the class is under android.widget.RemoteViews instead of appwidget, it is often used in the Android widgets development, and//primarily can be invoked across processes (Appwidget is uniformly run 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. Provides updated Appwidget status, gets installed Appwidget provides information and other related status Appwidgetmanager Myappwidgetmanager = Appwidgetmanager.getinstance (
    context);
  Myappwidgetmanager.updateappwidget (Mycomponentname, myremoteviews);
   /** * is invoked 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);
   /** * When an app widget instance is invoked the first time it is created.
   * For example, if the user adds two app widget instances, it is invoked only the first time.
   * If you need to open a new database or perform other settings that only need to happen once for all of the app widget instances, then this 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 work in onenabled (context), such as deleting a temporary database/@Override public void ondisabled {SYSTEM.OUT.P
    Rintln ("ondisabled");
  super.ondisabled (context);
   /** * will be invoked when each broadcast is received, and before the callback function above.
   * You don't usually need to implement this method, because the default Appwidgetprovider implementation filters all app widget broadcasts and invokes the above method appropriately.
   * Note: In Android 1.5, there is a known problem where the ondeleted () method is not invoked when invoked.
   * To circumvent this problem, you can implement OnReceive () as described in 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:i con= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Mainactivity "android:label=" main program "> <intent-filter> <action android:name=" android.intent.action.m AIN "/> <category android:name= android.intent.category.LAUNCHER"/> </intent-filter> ;/activity> <!--Testactivity class is a broadcast receiver because Testactivity inherits from Appwidgetprovider--> <receiver " . Testactivity "android:label=" Add Desktop Controls "> <intent-filter> <action android:name=" android.appwidget.a Ction.
        Appwidget_update "/> </intent-filter> <meta-data android:name=" Android.appwidget.provider " Android:resource= "@xml/my_appwidget"/> </receiver>

 </application> <uses-sdk android:minsdkversion= "7"/> </manifest>

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

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.