Home>

Here is a case:After adding appwidget to the desktop,After clicking appwidget, the appwidget text will be changed in a round-robin fashion.

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.widget.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 {
 //Customize an action name
 private static final string action_click_name="com.ljq.action.widget.click";
 private remoteviews rv;
 @override
 public void onupdate (context context, appwidgetmanager appwidgetmanager, int [] appwidgetids) {
  system.out.println ("onupdate");
  //Get the r.layout.main layout and operate the controls in the layout r.layout.main through the 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:every time it receives,Broadcasting is performed once for the end of one life cycle.
 //That is to say in the override appwidgetprovider class, declare global variables to make state judgments, //Every time the state changes, appwidgetprovider will re-initialize for you when receiving the second broadcast, that is, re-instantiate appwidgetprovider once.
 //Today I put a boolean value in it to be initialized to true, and observe the debugging to see that it is true every time I enter, so when you set the desktop component, //Global variable is declared in another entity class to judge it is OK,Never put it in this category.
 @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 remoteviews (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);
 }
}
  • Previous Write a small number guessing game in Java
  • Next C # operation IIS method collection