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

所属分类: 软件编程 / Android 阅读数: 1198
收藏 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应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多