最近公司项目写了一个widget,这里写了一个demo把主要的几个要点简要地总结了下
1、AndroidManifest.xml添加
<receiver android:name=".WidgetDemoProvider" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
2、xml文件夹内新建widget_provider.xml
minWidth: Wdiget的宽度
minHeight: Wdiget的高度
updatePeriodMillis: 更新的时间周期
initialLayout: Widget的布局文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
updatePeriodMillis="10000"
android:initialLayout="@layout/widget_layout">
</appwidget-provider>
3、layout文件夹内新建widget_layout.xml作为widget布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_centerHorizontal="true"
android:id="@+id/textLayout">
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/hello_world" />
</RelativeLayout>
</RelativeLayout>
4、继承AppWidgetProvider.java写WidgetDemoProvider.java代码如下
package com.example.widgetdemo;
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.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetDemoProvider extends AppWidgetProvider{
private static final int STATUS_ONE = 1;
private static final int STATUS_TWO = 2;
private RemoteViews mRemoteViews;
private RemoteViews subViews;
private RemoteViews subViews2;
private AppWidgetManager mAppWidgetManager;
private ComponentName thisWidget;
private Context mContext;
//AppWidgetProvider其实是一个BroadcastReceiver,所以可以在AndroidManifest.xml内配置监听广播action,在onReceive中作响应处理
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
//加载widget时会调用此方法,在此方法中可以做些初始化配置
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
mAppWidgetManager = appWidgetManager;
thisWidget = new ComponentName(context, WidgetDemoProvider.class);
//在widget中需要通过RemoteViews来对View进行操作
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
subViews = new RemoteViews(context.getPackageName(),R.layout.widget_text_layout1);
subViews2 = new RemoteViews(context.getPackageName(),R.layout.widget_text_layout2);
mContext = context;
initAppWidget();
mAppWidgetManager.updateAppWidget(thisWidget, mRemoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
handler.sendEmptyMessageDelayed(STATUS_ONE, 2000);
}
//删除组件时会调用
public void onDeleted(Context context, int[] appWidgetIds){
handler.removeMessages(STATUS_ONE);
handler.removeMessages(STATUS_TWO);
}
//设置控件响应,这里直接跳转setting的蓝牙设置
private void initAppWidget(){
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.android.settings","com.android.settings.bluetooth.BluetoothSettings");
intent.setComponent(componentName);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.textView, pendingIntent);
}
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case STATUS_ONE:
mRemoteViews.removeAllViews(R.id.textLayout);
mRemoteViews.removeAllViews(R.id.textLayout1);
mRemoteViews.removeAllViews(R.id.textLayout2);
mRemoteViews.addView(R.id.textLayout, subViews);
initAppWidget();
mAppWidgetManager.updateAppWidget(thisWidget, mRemoteViews);
handler.removeMessages(STATUS_ONE);
handler.removeMessages(STATUS_TWO);
handler.sendEmptyMessageDelayed(STATUS_TWO, 2000);
break;
case STATUS_TWO:
mRemoteViews.removeAllViews(R.id.textLayout);
mRemoteViews.removeAllViews(R.id.textLayout1);
mRemoteViews.removeAllViews(R.id.textLayout2);
mRemoteViews.addView(R.id.textLayout, subViews2);
initAppWidget();
mAppWidgetManager.updateAppWidget(thisWidget, mRemoteViews);
handler.removeMessages(STATUS_ONE);
handler.removeMessages(STATUS_TWO);
handler.sendEmptyMessageDelayed(STATUS_ONE, 2000);
break;
}
}
};
}
其中RemoteViews.setOnClickPendingIntent是设置按键跳转setting模块的,Handler内做了一个字体切换的动画特效,widget动画局限性很大,这里是通过layoutAnimation的方式来实现动画效果。替换的layout代码如下
widget_text_layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layoutAnimation="@anim/text_anim_out"
android:id="@+id/textLayout1">
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/hello_world" />
</RelativeLayout>
widget_text_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layoutAnimation="@anim/text_anim_in"
android:id="@+id/textLayout2">
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/hello_world" />
</RelativeLayout>
这样我们就可以通过RemoteViews.addView(R.id.textLayout, subViews); 切换View来切换布局触发android:layoutAnimation=”@anim/text_anim_out” 的动画效果。
其实widget还有一种简便的动画方式那就是setImageViewResource(int viewId, int srcId),srcId传值R.anim.xxx就可以对ImageView实现简单的动画切换效果了。
当然一个完整的widget可能还需要进行很多其他的处理比如起一个Service进行后台操作,这里就不进行扩展了。