Android控件AppWidgetProvider使用方法详解

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

介绍

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绑定监听器

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多