第三章--与Widget的爱恨情仇

Widget就是平时所说的桌面小部件,可以很方便的在桌面上进行操作,但是本质上它是一个广播接收器。直接看代码。

public class TestWidget extends AppWidgetProvider{
    public static final String WIDGET_BUTTON = "widget_button";
    @Override
    //接收广播
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
}

点进AppWidgetProvider,可以看到它是继承BroadcastReceiver的。onUpdate()在用户把小部件拖到桌面,创建Widget实例时会调用。onReceive()在接收广播的时候会被调用。所以在使用Widget的时候应该在AndroidManifest中注册.。且由于当程序开始的时候就监听,所以采用静态注册。

<receiver android:name=".TestWidget">
            <!--将该BroadcastReceiver当成桌面控件-->
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <!--指定桌面控件的meta-data-->
            <meta-data android:name="android.appwidget.provider"
                android:resource="@layout/widget_setting"/>
        </receiver>
  1. action里面表示接收系统发来的有关这个app的所有widget的消息。
  2. meta-data 指定了对应的资源文件。name 是指定metadata名,
    resource是指定对应的资源路径。那我们就把指定的资源文件贴出来。
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:initialLayout="@layout/layout_widget"
    android:minHeight="140dp"
    android:minWidth="140dp"
    android:previewImage="@drawable/smile"
    android:updatePeriodMillis="2000"
    android:widgetCategory="home_screen"
    >

</appwidget-provider>

简单解释一下:

  • initialLayout : 加载到桌面时对应的布局文件
  • minWidth : 最小宽度
  • minHeight : 最小高度
  • previewImage : 预览图片
  • updatePeriodMillis : 更新widget的时间间隔(ms)
  • widgetCategory : widget可以被显示的位置。home_screen表示可以将widget添加到桌面,keyguard表示widget可以被添加到锁屏界面。

加载在桌面的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/widget_text_View"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/wighet_button"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

第一个注意的地方,widget支持的控件有限,基本上包括以下几种:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView、AdapterView、Flipper。其他就显示不出来
前面准备工作做的差不多了,现在就是加载布局文件了。利用RemoteViews可以直接加载整个布局文件。在onUpdate()中的代码如下

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i(TAG, "onUpdate");
        //指定加载的页面布局文件
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
        Intent intent = new Intent();
        intent.setClass(context, TestWidget.class);
        intent.setAction(WIDGET_BUTTON);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);    remoteViews.setOnClickPendingIntent(R.id.wighet_button, pendingIntent);
        //
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

为Button设置点击事件是remoteViews.setOnClickPendingIntent(R.id.wighet_button, pendingIntent);传入的第一参数是Button的id,第二个是PendingIntent,,我的理解是不立即执行的意图。现在是想点击了Button然后发送广播,在onReceive()中更新TextView的内容。

    @Override
    //接收广播
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.i(TAG, "onReceive");
        if (intent != null && TextUtils.equals(intent.getAction(), WIDGET_BUTTON)){
            Log.i(WIDGET_BUTTON, "I am clicked");
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
            //设置文字的内容
            remoteViews.setTextViewText(R.id.widget_text_View, "be clicked");

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            //将AppWidgetProvider的子类实例包装成ComponentName
            ComponentName componentName = new ComponentName(context, TestWidget.class);

            appWidgetManager.updateAppWidget(componentName, remoteViews);

        }
    }

第二个注意的地方,RemoteViews 绝对不能复用,据说Binder data size limit is 512k,由于传输到appWidget进程中的Binder最大数据量是512K,并且RemoteView也不会每次清理, 所以如果每次都使用同一个RemoteView进行传输会因为溢出而报错。所以必须每次重新建一个RemoteView来传输。
这样一来一去,基本上就实现了一个可以操作的widget。如果想接收其他地方发的广播,需要在AndroidManifest中注册,然后在onReceive()中进行相关操作。
此时应该有图,但是图不见了,自行脑补一个TextView下有个Button。


当绑定多个Button或者view设置点击事件的时候,如果只是设置Intent的Action不同,好像不能分辨,就是所有的按钮都是一个功能,我的做法是将PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);的第二个参数设置的不同,这样就可以分辨。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值