android widget 开发,android widget

什么是AppWidget?AppWidget就是我们平常在桌面上见到的那种一个个的小窗口,利用这个小窗口可以给用户提供一些方便快捷的操作。

创建简单的AppWidget

2b5280ec4de8

添加AppWidgetProviderInfo Metadata

在res文件夹下新建一个名字为xml的文件夹,然后在xml目录下创建一个名为appwidget.xml的文件:

xmlns:android="http://schemas.android.com/apk/res/android"

android:minWidth="200dp"

android:minHeight="100dp"

android:updatePeriodMillis="0"

android:previewImage="@drawable/cd01"

android:initialLayout="@layout/widget_layout">

为appwidget定义layout

在layout文件夹下面新建一个widget_layout.xml文件,在这个文件中描述了appWidget的控件和布局等等信息:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/widget_text"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="10sp"

android:text="widget"/>

android:id="@+id/widget_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button"/>

创建AppWidgetProvider的子类

public class MyWidgetProvider extends AppWidgetProvider {

public static final String BROADCAST_STR = "com.kevinwang.widget";

public static final String MY_WIDGET_PROVIDER = "MyWidgetProvider";

@Override

public void onReceive(Context context, Intent intent) {

Log.i(MY_WIDGET_PROVIDER,"==onReceive()==");

super.onReceive(context,intent);

//接受广播事件

if (intent != null && TextUtils.equals(intent.getAction(), BROADCAST_STR)) {

//只能通过远程对象来设置appwidget中的控件状态

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

//通过远程对象将textView的文字设置为”hello”

remoteViews.setTextViewText(R.id.widget_text, "hello");

//获得appwidget管理实例,用于管理appwidget以便进行更新操作

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

//获得所有本程序创建的appwidget

ComponentName componentName = new ComponentName(context,MyWidgetProvider.class);

//更新appwidget

appWidgetManager.updateAppWidget(componentName, remoteViews);

}

}

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

//到达指定的更新时间或者当用户向桌面添加AppWidget时被调用

Log.i(MY_WIDGET_PROVIDER,"==onUpdate()==");

super.onUpdate(context, appWidgetManager, appWidgetIds);

//创建一个Intent对象

Intent intent = new Intent();

intent.setClass(context, MyWidgetProvider.class);

intent.setAction(BROADCAST_STR);

//设置pendingIntent

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

//Retrieve a PendingIntent that will perform a broadcast

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

//为button按钮绑定一个事件便于发送广播

remoteViews.setOnClickPendingIntent(R.id.widget_btn,pendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

}

@Override

public void onDeleted(Context context, int[] appWidgetIds) {

//删除一个AppWidget时调用

Log.i(MY_WIDGET_PROVIDER,"==onDelete()==");

super.onDeleted(context, appWidgetIds);

}

@Override

public void onEnabled(Context context) {

//AppWidget的实例第一次被创建时调用

Log.i(MY_WIDGET_PROVIDER,"==onEnabled()==");

super.onEnabled(context);

}

@Override

public void onDisabled(Context context) {

//删除一个AppWidget时调用

Log.i(MY_WIDGET_PROVIDER,"==onDisabled()==");

super.onDisabled(context);

}

}

在AndroidManifest中进行声明

android:resource="@xml/appwidget"/>

可以看到在配置文件里面定义了一个receiver,它的名字是上面创建处理控件代码的那个类。

intent-filter中的action是系统自带的用于更新所有appwidget的广播动作。

meta-data标签是一个描述我们创建appwidget的元数据,其中的android:name="android.appwidget.provider"是固定的,

android:resource="@xml/appwidget"指定创建的appWidget的布局信息。这样程序就知道到哪里去初始化这些appWidget啦。

AppWidget与程序交互

appwidget运行的进程和我们创建的应用不在一个进程中,所以我们也就不能像平常引用控件那样来获得控件的实例。这个时候就需要用到RemoteViews。

RemoteViews,顾名思义,它是一个远程视图。App Widget中的视图,都是通过RemoteViews表现的。

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

//为button按钮绑定一个事件便于发送广播

remoteViews.setOnClickPendingIntent(R.id.widget_btn,pendingIntent);

上面出现的pendingIntent又是用来做什么的呢?PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是把PendingIntent放入一个新的进程。通过触发事件去触发这个PendingIntent。如果我们把Intent看成一封信,那么PendingIntent就是一封被信封包裹起来的信。这封信在remoteViews.setOnClickPendingIntent()中被“邮寄”到了appwidget, 当appwidget中的按钮单击时他知道将这封信打开,并执行里面的内容。

//创建一个Intent对象

Intent intent = new Intent();

intent.setClass(context, MyWidgetProvider.class);

intent.setAction(BROADCAST_STR);

//设置pendingIntent

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

在onUpdate方法中为AppWidget绑定事件

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

//到达指定的更新时间或者当用户向桌面添加AppWidget时被调用

//创建一个Intent对象

Intent intent = new Intent();

intent.setClass(context, MyWidgetProvider.class);

intent.setAction(BROADCAST_STR);

//设置pendingIntent

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

//Retrieve a PendingIntent that will perform a broadcast

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

//为button按钮绑定一个事件便于发送广播

remoteViews.setOnClickPendingIntent(R.id.widget_btn,pendingIntent);

//更新Appwidget

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

super.onUpdate(context, appWidgetManager, appWidgetIds);

}

在onReceive中接受广播并更新AppWidget

public void onReceive(Context context, Intent intent) {

//接受广播事件

if (intent != null && TextUtils.equals(intent.getAction(), BROADCAST_STR)) {

Log.i("onReceive()", "enter if block!");

//只能通过远程对象来设置appwidget中的控件状态

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

//通过远程对象将textView的文字设置为”hello”

remoteViews.setTextViewText(R.id.widget_text, "hello");

//获得appwidget管理实例,用于管理appwidget以便进行更新操作

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

//获得所有本程序创建的appwidget

ComponentName componentName = new ComponentName(context,MyWidgetProvider.class);

//更新appwidget

appWidgetManager.updateAppWidget(componentName, remoteViews);

}

else {

super.onReceive(context,intent);

}

}

AppWidget的生命周期

当我们添加AppWidget时

2b5280ec4de8

点击其中的按钮时

2b5280ec4de8

移除控件时

2b5280ec4de8

从上面的结果中可以看出,AppWidgetProvider的其它4个生命周期函数的执行都是由onReceiver分发下去的。

在AppWidgetProvider实现中已经对接收到的

ActionAppWidgetManager.ACTION_APPWIDGET_UPDATE,

AppWidgetManager.ACTION_APPWIDGET_DELETED,

AppWidgetManager.ACTION_APPWIDGET_ENABLED以及

AppWidgetManager.ACTION_APPWIDGET_DISABLED做了处理,分别执行onUpdate()/ onDeleted() / onEnabled() / onDisabled()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值