首页 > 系统 > Android > 正文

Android控件AppWidgetProvider使用方法详解

2019-12-12 02:07:16
字体:
来源:转载
供稿:网友

介绍

AppWidgetProvider是Android中提供的用于实现桌面小工具的类,其本质是一个广播,即BroadcastReceiver,在实际的使用中,把AppWidgetProvider当成一个BroadcastReceiver即可

1. 为AppWidget提供一个文件定义小控件的基本配置信息

在资源文件夹res目录下新建xml文件夹,添加app_widget_provider_info.xml文件内容为:

<?xml version="1.0" encoding="utf-8"?>  <!--小控件宽高-->  <!--android:minWidth="40dp"-->  <!--android:minHeight="40dp"-->  <!--更新时间-->  <!--android:updatePeriodMillis="86400000"-->  <!--用于指定预览图片。即搜索到widget时,查看到的图片。若没有设置的话,系统为指定一张默认图片。-->  <!--android:previewImage="@drawable/widget_flashlight"-->  <!--widget 添加到手机主屏幕中的layout-->  <!--android:initialLayout="@layout/flash_light_widget"-->  <!--android:resizeMode : widget可以被拉伸的方向。horizontal表示可以水平拉伸,vertical表示可以竖直拉伸-->  <!--android:resizeMode="horizontal|vertical"--><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  android:minWidth="40dp"  android:minHeight="40dp"  android:updatePeriodMillis="86400000"  android:previewImage="@drawable/ic_launcher"  android:initialLayout="@layout/widget_layout"  android:resizeMode="horizontal|vertical"></appwidget-provider>

2. 创建一个WidgetProvider继承自AppWidgetProvider;

public class MyAppWidgetProvider extends AppWidgetProvider {  //没接收一次广播消息就调用一次,使用频繁   public void onReceive(Context context, Intent intent) {    super.onReceive(context, intent);  }  //每次更新都调用一次该方法,使用频繁   public void onUpdate(Context context, AppWidgetManager appWidgetManager,             int[] appWidgetIds) {    super.onUpdate(context, appWidgetManager, appWidgetIds);  }  //没删除一个就调用一次   public void onDeleted(Context context, int[] appWidgetIds) {    super.onDeleted(context, appWidgetIds);  }  //当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用   public void onEnabled(Context context) {    super.onEnabled(context);  }  //当最后一个该Widget删除是调用该方法,注意是最后一个   public void onDisabled(Context context) {    super.onDisabled(context);  }}

3. 为 WidgetProvider创建一个布局文件

布局为常见布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="64dp"  android:layout_height="64dp"   >  <ImageButton    android:id="@+id/widget_led"    android:layout_margin="2dp"    android:background="@drawable/widget_led"    android:src="@drawable/ic_launcher"    android:scaleType="center"    android:layout_width="64.0dip"    android:layout_height="64.0dip" /></RelativeLayout>

4. 注册Manifest.xml

配置基本和广播一样,使用receiver 节点,meta-data 节点的name 为固定格式,resource为第一步定义的配置信息,intent-filter节点第三个action必须提供

<receiver android:name=".jf.jfclean.widget.FlashLightWidget">      <intent-filter>        <action android:name="action_led_on" />        <action android:name="action_led_off" />        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />      </intent-filter>      <meta-data        android:name="android.appwidget.provider"        android:resource="@xml/flash_light_widget_info" />    </receiver>

5. 使用PendingIntent和RemoteViews对AppWidget绑定监听器,使用RemoteViews在MyAppWidgetProvider的onUpdate()方法中为Botton绑定监听器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表