android笔记01 AppWidgetProvider

来源:互联网 发布:满汉大餐方便面 知乎 编辑:程序博客网 时间:2024/06/10 21:33

开发一个安卓应用程序桌面小部件(Widget).

首先,创建一个安卓应用程序.

在res/layout目录下创建一个widget的布局文件.

例如,widget.xml

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/text1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/text1"        android:layout_below="@+id/text1"        android:layout_marginTop="22dp"        android:text="Button" /></RelativeLayout>
在res/xml目录下新建一个xml文件,用来定义widget的一些信息

例如,appwidget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="129dp"    android:minHeight="72dp"    android:updatePeriodMillis="30000"    android:initialLayout="@layout/activity_main"    />

接着,在项目中新建一个类,这个类要继承AppWidgetProvider类,并且要实现其中的onReceive onUpdate onDeleted onEnabled onDisabled这五个方法(非必要,在本例中仅实现update和receive).

首先在程序里定义了一个自己的action的名称.

private static String MYACTION = "com.example.exampleappwigtprovider.MYACTION";

然后在onUpdate中通过新建一个 Intent 来创建一个PendingIntent 再通过一个RemoteViews对象来获取到widget的整个View.并且把其中的TextVIew文本更改,Button添加一个OnclickPendingIntent()事件.然后通过appWidgetManager来修改widget界面.

@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {Intent i = new Intent();i.setAction(myaction);PendingIntent pi = PendingIntent.getBroadcast(context, -1, i, 0);RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget);rv.setOnClickPendingIntent(R.id.button1, pi);rv.setTextViewText(R.id.text1, "haha");appWidgetManager.updateAppWidget(appWidgetIds, rv);}
然后在程序中重写onReceive(),自定义接收到广播后的行为.

@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(MYACTION)) {Toast.makeText(context, MYACTION, Toast.LENGTH_SHORT).show();}else {super.onReceive(context, intent);}}

同样的在onReceive()中也可以如果onUpdate中一样,对widget的外观进行改变,方法一样,先取得RemoteViews,然后改变其中的view,再用appWidgetManger更新界面.

唯一的区别是在onReceive方法中没有int[] appWidgetIds的形参了,所以appWidgetManager.updateAppWidget(appWidgetIds, rv);中的appWidgetIds无法拿到.

解决的办法是使用该方法的另一种重载.首先定义一个ComponentName对象,通过context和自定义的appWidgetProvider类来获得,然后调用

appWidgetManager.updateAppWidget(cn, rv);

ComponentName cn = new ComponentName(context, ExampleAppWigtProvider.class);appWidgetManager.updateAppWidget(cn, rv);


最后,要在AndroidManifest.xml中添加receiver的注册信息(与activity同级).

 <receiver android:name="com.example.exampleappwigtprovider.ExampleAppWigtProvider">        <intent-filter>            <action android:name="android.appwidget.action.APPWIDGET_UPDATE">            </action>        </intent-filter>        <intent-filter>            <action android:name="com.example.exampleappwigtprovider.myaction">            </action>        </intent-filter>        <meta-data android:name="android.appwidget.provider"            android:resource="@xml/appwidget_info">        </meta-data>        </receiver>
其中,首先定义receiver的name,intent-filter表示recer只接收哪种action(虽然没有显示说明,但是AppWidgetManager.ACTION_APPWIDGET_DELETED/ AppWidgetManager.ACTION_APPWIDGET_ENABLED以及AppWidgetManager.ACTION_APPWIDGET_DISABLED这些action都会被接收到).

meta-data即是receiver的元数据,通过之前的xml文件定义.

在安装程序后,即可通过发现自定义部件中多出了自己的widget.





原创粉丝点击