当前位置:操作系统 > 安卓/Android >>

android 转 比较好的widget讲解

1.创建AppWidget布局,包含两个TextView用来显示内容: 
Xml代码  
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
    android:orientation="vertical">  
    <TextView android:layout_height="wrap_content"  
        android:layout_width="fill_parent" android:id="@+id/tv1"  
        android:textColor="#FF0000" android:textSize="24sp" android:textStyle="bold"  
        android:text="-1"></TextView>  
    <TextView android:layout_height="wrap_content" android:id="@+id/tv2"  
        android:textSize="24sp" android:textStyle="bold" android:textColor="#00FF00"  
        android:layout_width="fill_parent" android:text="-2"></TextView>  
</LinearLayout>  
 
2.在res下创建xml目录,再在xml目录里面创建AppWidget信息xml文件: 
2.1新建xml文件时,type选择AppWidget Provider。 
 
 
2.2填充属性: 
 
宽高的计算公式为:占用屏幕格数*74-2 
Update period millis:设置为0,手动刷新。根据实验,设置不为0时,至少在2.2上系统根本不按照设置的值刷新,还是自己控制刷新时机好了。 
Initial layout:就添控件要使用的布局。 
Configure暂时不用,留空。 
3.创建AppWidgetDemo类: 
 
重载AppWidgetProvider中的所有函数,每个函数里面增加输出语句,以查看调用顺序。 
Java代码  
public class AppWidgetDemo extends AppWidgetProvider {  
  
    @Override  
    public void onDeleted(Context context, int[] appWidgetIds) {  
        // TODO Auto-generated method stub  
        super.onDeleted(context, appWidgetIds);  
        Log.e("AppWidgetDemo", "onDeleted");  
    }  
  
    @Override  
    public void onDisabled(Context context) {  
        // TODO Auto-generated method stub  
        super.onDisabled(context);  
        Log.e("AppWidgetDemo", "onDisabled");  
    }  
  
    @Override  
    public void onEnabled(Context context) {  
        // TODO Auto-generated method stub  
        super.onEnabled(context);  
        Log.e("AppWidgetDemo", "onEnabled");  
    }  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        // TODO Auto-generated method stub  
        super.onReceive(context, intent);  
        Log.e("AppWidgetDemo", "onReceive,Action:" + intent.getAction());  
    }  
  
    @Override  
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
        // TODO Auto-generated method stub  
        super.onUpdate(context, appWidgetManager, appWidgetIds);  
        Log.e("AppWidgetDemo", "onUpdate,Count:" + appWidgetIds.length);  
    }  
  
}  
 
4.在AndroidManifest.xml文件中声明此Widget: 
添加一个Receiver,其name为AppWidgetDemo类的类名。 
Xml代码  
<receiver android:name="AppWidgetDemo"></receiver>  
 
为此Receiver添加Intent filter,接收系统发出android.appwidget.action.APPWIDGET_UPDATE的Intent。 
Xml代码  
<receiver android:name="AppWidgetDemo">  
    <intent-filter>  
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>  
    </intent-filter>            
</receiver>  
 
此外还要为此receiver添加meta-data信息,以告知系统相关的AppWidgetProvider信息: 
Xml代码  
<meta-data android:name="android.appwidget.provider"  
                android:resource="@xml/widget_info_demo"></meta-data>  
 
meta-data的name是约定好的android.appwidget.provider,resource则是第2步创建的AppWidget信息xml文件。 
 
5.至此AppWidget已经可用了,安装到模拟器上看下运行流程。 
5.1添加一个Widget到桌面上: 
onEnabled被呼叫:按照说明,当桌面上出现第一个此Widget的实例时,此函数被呼叫。 
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_ENABLED 
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量为1 
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE 
 
5.2再添加一个Widget到桌面上: 
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量仍然为1,而不是2。 
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE 
 
5.3从桌面上移除一个Widget: 
onDeleted:每个实例被移除时都会被呼叫 
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED 
 
5.4再从桌面上移除一个Widget: 
onDeleted:仍然执行 
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED 
onDisabled:因为是最后一个活动的实例被移除了,所以被呼叫。 
onReceive,Action:android.appwidget.action.APPWIDGET_DISABLED 
 
6.刷新AppWidget 
6.1在onUpdate()中刷新: 
onUpdate在AppWidget放到桌面时会被调用,在Update period millis达到时可能会被调用。 
Java代码  
RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.widget_layout_demo);  
appWidgetView.setTextViewText(R.id.tv1, String.valueOf(mCount));  
appWidgetView.setTextViewText(R.id.tv2, String.valueOf(mCount));  
appWidgetManager.updateAppWidget(appWidgetIds, appWidgetView);  
 
先获取一个RemoteViews,也就是AppWidget的布局所对应的View; 
使用指定的Id更新要更新的控件; 
更新整个RemoteViews,此时就可以更新AppWidget内容了。 
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,