Android Desktop Component App widget complete case _android

Source: Internet
Author: User

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

Here to simulate a case: add Appwidget to the desktop, after clicking Appwidget appwidget text will change

Main.xml Layout file:

<?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:id=" @+id/tv
  android:layout_width= "Fill_parent"
  android: layout_height= "Wrap_content" 
  android:text= "program entry" 
  android:textsize= "50dip"/>
</linearlayout >

Res/xml/my_appwidget.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android=
 "http://" Schemas.android.com/apk/res/android "
 android:minwidth=" 120DP " 
 android:minheight=" 60DP "
 android: updateperiodmillis= "1000"
 android:initiallayout= "@layout/main" >
</appwidget-provider>

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" >
  <receiver android:name =". Testactivity ">
   <meta-data android:name=" Android.appwidget.provider "
    android:resource=" @xml/my_ Appwidget ">
   </meta-data>
   <intent-filter>
    <action android:name=" COM. Ljq. ACTION. Widgets. Click "></action>
    <action android:name=" Android.appwidget.action.APPWIDGET_UPDATE "/>
   </intent-filter>
  </receiver>
 </application>
 <uses-sdk android:minsdkversion = "7"/>
</manifest>

Variable class Utiltool: Used to control text changes:

Package com.ljq.activity;
public class Utiltool {public
 static Boolean ischange=true;
}

Testactivity class, inherited from Appwidgetprovider:

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; public class Testactivity extends Appwidgetprovider {//Custom one ACTION name private static final String Action_click_name = "C Om. Ljq. ACTION. Widgets.
 Click ";
 Private Remoteviews RV; @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {System.out.pri
  Ntln ("OnUpdate"); Gets the R.layout.main layout, operates on the controls in the layout R.layout.main through class remoteviews/*RV = new Remoteviews (Context.getpackagename (),
  R.layout.main);
  Intent Intentclick = new Intent (action_click_name);
  Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, Intentclick, 0);
  Rv.setonclickpendingintent (r.id.tv, pendingintent);
  ComponentName cmp = new ComponentName (context, testactivity.class); AppWidgetmanager Myappwidgetmanager = appwidgetmanager.getinstance (context);
  Myappwidgetmanager.updateappwidget (CMP, RV); */FINAL int N = appwidgetids.length;
   for (int i = 0; i < N; i++) {int appwidgetid = appwidgetids[i];
  Updateappwidget (context, Appwidgetmanager, Appwidgetid);
 }//appwidget life cycle: Each time it is received, the broadcast executes once for the end of a lifecycle. In other words, we declare the global variable in the rewrite Appwidgetprovider class to do the State judgment.
 Each time the state changes appwidgetprovider to receive a second broadcast, it is reinitialized for you, that is, a appwidgetprovider was instantiated again.
 Today, I put a Boolean value in it. Initialize to true, observe debugging see that every entry is true so you are setting up desktop components,//global variables to declare it in another entity class to judge is no problem, avoid in this class.
  @Override public void OnReceive (context context, Intent Intent) {System.out.println ("onreceive");
  if (rv = = null) {RV = new Remoteviews (Context.getpackagename (), r.layout.main); } if (Intent.getaction (). Equals (Action_click_name)) {if (Utiltool.ischange) {Rv.settextviewtext (r.id.tv, "abc")
   ;
   else {rv.settextviewtext (r.id.tv, "123"); } Utiltool.ischange =!
   Utiltool.ischange; Appwidgetmanager AppwidgetmAnger = appwidgetmanager.getinstance (context);
   int[] Appids = appwidgetmanger.getappwidgetids (new componentname (context, testactivity.class));
  Appwidgetmanger.updateappwidget (Appids, RV);
  }else{super.onreceive (context, intent); } private void Updateappwidget (context context, Appwidgetmanager Appwidgemanger, int appwidgetid) {RV = new Remot
  EViews (Context.getpackagename (), r.layout.main);
  Intent Intentclick = new Intent ();
  Intentclick.setaction (Action_click_name);
  Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, Intentclick, 0);
  Rv.setonclickpendingintent (r.id.tv, pendingintent);
 Appwidgemanger.updateappwidget (Appwidgetid, RV);

 }
}

I hope this article will help you with your 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.