发布于 2017-09-10 12:16:47 | 172 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android控件AppWidgetProvider的使用方法详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

介绍

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

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务