Android开发之App widget用法实例分析

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

本文实例讲述了Android开发之App widget用法。分享给大家供大家参考,具体如下:

放在桌面上的控件叫做——App widget,例如可以在桌面上添加按钮、图片等等控件,例如桌面播放器的控制面板

AppWidgetProviderInfo对象,它为App Widget提供元数据,包括布局、更新频率等等数据,这个对象不是由我们自己生成的,而是由android自己定义配置完成,这个对象被定义在XML文件中

1、定义AppWidgetProviderInfo对象,在res/xml文件夹当中定义一个名为widget_config.xml文件

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="300dp"
  android:minHeight="72dp"
  android:updatePeriodMillis="0"
  android:initialLayout="@layout/widget_ui"
>
</appwidget-provider>

备注:建立的文件夹名一定是xml,因为只有这样才能被R识别

2、AppWidgetProvider定义了App Widget的基本生命周期

public class MyWidgetProvider extends AppWidgetProvider {
  public static int Tag;
  public int max;
  public int current;
  @Override
  public void onEnabled(Context context) {
    super.onEnabled(context);
    System.out.println("第一次被创建时调用这个方法");
  }
  @Override
  public void onDisabled(Context context) {
    System.out.println("当最后一个App Widget被删除时调用该方法");
  }
  @Override
  public void onReceive(Context context, Intent intent) {
  //调用父类的onReceive方法不能少,否则就无法监听到onUpdate事件了
    super.onReceive(context, intent);
    System.out.println("接收广播事件");
  }
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {
      System.out.println("在到达指定的更新时间之后或者当用户向桌面添加App Widget时调用这个方法");
      for(int i = 0; i < appWidgetIds.length; i++){
        Intent intent = new Intent(context, HB.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        //R.layout.widget_ui指的是显示在桌面上的控件布局
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_ui);
        //R.id.widgetButton指的是为桌面控件按钮绑定事件
        remoteViews.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);
        //updateAppWidget方法更新remoteViews
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
      }
    }
  }
  @Override
  public void onDeleted(Context context, int[] appWidgetIds){
    System.out.println("App Widget被删除时调用这个方法");
  }
}

3、添加一个布局文件res/layout/widget_ui.xml(在桌面上显示的内容)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
  <Button
  android:id="@+id/widget_BT_Up"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="Value++"/>
  <Button android:id="@+id/widget_BT_Down"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Value--"
  android:layout_weight="1"/>
</LinearLayout>

4、在AndroidManifest.xml文件中添加reseiver标签

android:resource="@xml/widget_config" 指明显示widget_config.xml是appwidget的属性初始化设置
android:name="android.appwidget.action.APPWIDGET_UPDATE" 是android系统提供判定是appwidget的处理方式
android:name=".MyWidgetProvider" 表示处理的类,即继承了AppWidgetProvider类的类

<receiver android:name=".MyWidgetProvider" android:label="myWIdget" android:icon="@drawable/icon">
  <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  </intent-filter>
  <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config"/>
</receiver>

备注:App Widget和我们应用程序运行在不同的进程中(App Widget当中的View运行在Home Screen进程中),因此要用到RemoteViews和PendingIntent这两个类来操控桌面的控件

如果你的onDelete、onUpdate等事件没有触发,那么一个重要的原因是,你override了onReceive事件,但是又没有调用super.onReceive(),所以导致这之后的事件都不会触发,AppWidgetProvider的事件处理机制是,onRecieve首先触发,然后由onReceive去触发后续事件。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

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

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多