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

所属分类: 软件编程 / Android 阅读数: 1177
收藏 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程序设计有所帮助。

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

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多