android除了普通的app应用外,还提供了桌面组件,那么怎么开发一个桌面组件呢?来看一下吧。
我们做这样一个功能,通过桌面组件获取imel,然后调用webserver,通过imel获得用户的相应信息,比如姓名,并显示在桌面。
一、新建工程
新建的工程为普通android工程
图标和activity不要选,因为我们不需要他们
其它的没什么要注意的了。
二、xml配置
桌面组件需要的xml如下所示:
main.xml和AndroidManifest.xml就不多说了,一个是布局,一个是主要的配置。这两个在普通app应用中也是有的。这里说一下widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="80dip"
android:minHeight="25dip"
android:initialLayout="@layout/main"/>
说明一下,这里的minWidth和minHeight指定桌面应用的大小。initialLayout指定布局xml。
三、实现代码
桌面组件的实现只需要继承AppWidgetProvider类即可。
可以覆写的方法有
public
void
onReceive(Context context, Intent intent) 用来接收广播消息
public
void
onUpdate(Context context, AppWidgetManager appWidgetManager,
int
[] appWidgetIds) 每次更新都会调用,从桌面拖出桌面组件也会调用
public
void
onDeleted(Context context,
int
[] appWidgetIds) 删除的时候调用
public
void
onEnabled(Context context) 从桌面拖出时会调用,多次拖出只调用一次
public
void
onDisabled(Context context) 最后一个组件删除时调用该方法
我们这里只是覆写onUpdate
有一点要注意:和普通app应用的service一样,http请求发送不能写在主线程里,需要启动一个新的线程进行处理。
下面来看一下实现的例子
package com.wlf.selfAware;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wlf.selfAware.cache.CacheLoader;
import com.wlf.selfAware.cache.NameCacheFactory;
import com.wlf.selfAware.cache.NameCacheLoader;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imel=tm.getDeviceId();
System.out.println("--------------------imel:"+imel+"-----------------------");
Aware aware=new Aware(context,appWidgetManager);
Thread thread=new Thread(aware);
thread.start();
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private class Aware implements Runnable{
Context context;
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
String name;
public Aware(Context context,AppWidgetManager appWidgetManager){
this.context=context;
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
thisWidget = new ComponentName(context,Widget.class);
}
public void run() {
String name="";
CacheLoader<String,Properties> nameCacheLoader=new NameCacheLoader(new NameCacheFactory());
HttpClient httpclient = new DefaultHttpClient();
String uri="http://www.网址不告诉你:80/Service/rest/vip/pad/name/"+<span style="font-size:14px;"><span><span></span></span></span>imel;<span style="font-size:14px;"><span><span></span></span></span>
HttpGet request = new HttpGet(uri);
request.addHeader("Accept", "text/xml");
try{
HttpResponse response = httpclient.execute(request);
String rs=parseResponse(response);
System.out.println("--------------返回值:"+rs+"------------------");
JSONObject json=(JSONObject) JSON.parse(rs);
name=(String) json.get("obj");
System.out.println("--------------返回值:"+json.get("obj")+"------------------");
//将姓名存起来
System.out.println("--------------将获得的名字缓存起来------------------");
nameCacheLoader.writeResource(this.context, "name", name);
System.out.println("--------------name:"+name+"------------------");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getStackTrace());
}
if(name!=null && !name.equals("")){
remoteViews.setTextViewText(R.id.myName, name);
}
else{
String oldName="";
try {
System.out.println("--------------取得上次成功获取的名字------------------");
oldName = nameCacheLoader.readResource(context, "name");
System.out.println("--------------oldName:"+oldName+"------------------");
} catch (Exception e) {
e.printStackTrace();
}
if(oldName!=null && !oldName.equals(""))
remoteViews.setTextViewText(R.id.myName, oldName);
else
remoteViews.setTextViewText(R.id.myName, "无用户或未联网");
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
public String parseResponse(HttpResponse response) throws Exception {
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(instream));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
String jsonString = sb.toString();
instream.close();
return jsonString;
}
}