appwidget是桌面小部件 类似于天气预报那样的,是broadcastreceiver的子类,建立myappwidget expends appwidget
首先完成注册
<!-- 注册AppWidget -->
<receiver android:name="cn.tedu.android_day12_appwidget.MyAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="ACTION_BUTTON2_CLICKED"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_meta"/>
</receiver>
然后建立appwidget_item的布局文件,并在myappwidget中显现出来,例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="5dp"
android:text="@string/hello_world"
android:textSize="16sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
</RelativeLayout>
第三步进行对小部件显示的设置,例如天气预报的部件拉出来的大小,在res中建立xml文件夹,并在里面建立appwidget_data的xml文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="40dp"
android:initialLayout="@layout/appwidget_main">
</appwidget-provider>
最后在myappwidget中编写代码:onupdata方法,比较常用
public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds) {
Log.i("info", "onUpdate.."+Arrays.toString(appWidgetIds));
//给button1按钮添加点击意图
//1. manager
//2. 创建远程视图对象RemoteViews
RemoteViews views = new RemoteViews(
context.getPackageName(), R.layout.appwidget_main);
//更新views中控件的内容
views.setTextViewText(R.id.textView1, "Hello Android");
views.setTextColor(R.id.textView1, Color.RED);
//给button1添加点击意图
Intent i1 = new Intent(context, MainActivity.class);
PendingIntent pi1 = PendingIntent.getActivity(context, 0, i1,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.button1, pi1);
//给button2添加点击意图
//Intent i2 = new Intent("ACTION_BUTTON2_CLICKED");
//PendingIntent pi2 = PendingIntent.getBroadcast( context, 0, i2, PendingIntent.FLAG_UPDATE_CURRENT);
// views.setOnClickPendingIntent(R.id.button2, pi2);
//3 updateAppWidget
manager.updateAppWidget(appWidgetIds, views);
}
如果需要直接在生成的小部件上进行更改,最好进行发送广播,appwidget是广播接收者的子类
//给button2添加点击意图
//Intent i2 = new Intent("ACTION_BUTTON2_CLICKED");
//PendingIntent pi2 = PendingIntent.getBroadcast( context, 0, i2, PendingIntent.FLAG_UPDATE_CURRENT);
// views.setOnClickPendingIntent(R.id.button2, pi2);然后在
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//接收广播
String action=intent.getAction();
if(action.equals("ACTION_BUTTON2_CLICKED")){
//点击了button2 更新AppWidget
//1. manager
AppWidgetManager manager =
AppWidgetManager.getInstance(context);
//2. RemoteViews
RemoteViews views = new RemoteViews(
context.getPackageName(),
R.layout.appwidget_main);
int[] colors = {Color.RED, Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GREEN, Color.YELLOW, Color.WHITE};
views.setTextColor(R.id.textView1, colors[new Random().nextInt(colors.length)]);
//发送http请求 查询数据库 xxxx
//3. manager.updateAppWidget()
//组件名对象 指向目标组件
ComponentName name=new ComponentName(
context, MyAppWidget.class);
manager.updateAppWidget(name, views);
}
}