App Widgets
App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host.

翻译:App Widgets是一个显示在别的application中(比如显示在桌面程序)的微型application views,并且定期接受更新。这个views在用户界面被叫Widgets,你可以发布一个自己应用的Widget。Widget的application称为App Widget host。

1、AppWidget 框架类

2、AppWidget 框架的主要类介绍

3、创建步骤和代码

1、AppWidget 框架类

  • 1、AppWidgetProvider :继承自 BroadcastRecevier , 在AppWidget 应用 update、enable、disable 和 delete 时接收通知。其中,onUpdate、onReceive 是最常用到的方法,它们接收更新通知。
  • 2、 AppWidgetProvderInfo:描述 AppWidget 的大小、更新频率和初始界面等信息,以XML 文件形式存在于应用的 res/xml/目录下。
  • 3、AppWidgetManger :负责管理 AppWidget ,向 AppwidgetProvider 发送通知。
  • 4、RemoteViews :一个可以在其他应用进程中运行的类,向 AppWidgetProvider 发送通知。

2、AppWidget 框架的主要类介绍

1) AppWidgetManger 类

  • bindAppWidgetId(int appWidgetId, ComponentName provider)
    通过给定的ComponentName 绑定appWidgetId
  • getAppWidgetIds(ComponentName provider)
    通过给定的ComponentName 获取AppWidgetId
  • getAppWidgetInfo(int appWidgetId)
    通过AppWidgetId 获取 AppWidget 信息
  • getInstalledProviders()
    返回一个List<AppWidgetProviderInfo>的信息
  • getInstance(Context context)
    获取 AppWidgetManger 实例使用的上下文对象
  • updateAppWidget(int[] appWidgetIds, RemoteViews views)
    通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
  • updateAppWidget(ComponentName provider, RemoteViews views)
    通过 ComponentName 对传进来的 RemoeteView 进行修改,并重新刷新AppWidget 组件
  • updateAppWidget(int appWidgetId, RemoteViews views)
    通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件

2) 继承自 AppWidgetProvider 可实现的方法为如下:

    • 1、onDeleted(Context context, int[] appWidgetIds)
    • 2、onDisabled(Context context)
    • 3、onEnabled(Context context)
    • 4、onReceive(Context context, Intent intent)
      Tip:因为 AppWidgetProvider 是继承自BroadcastReceiver  所以可以重写onRecevie 方法,当然必须在后台注册Receiver
    • 5、onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    • 一般来说都是重写onUpdate就够了

3.应用创建Widget步骤如下

1.创建一个继承了AppWidgetProvider的类,AppWidgetProvider是BroadcastReceiver。所以创建的类也是BroadcastReceiver

2.在清单文件中注册该类,需要注意的是该类的action为:android.appwidget.action.APPWIDGET_UPDATE

  1. <receiver android:name=".receiver.MyWidget">
  2. <intent-filter>
  3. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  4. </intent-filter>
  5.  
  6. <meta-data
  7. android:name="android.appwidget.provider"
  8. android:resource="@xml/myappwidget_info"/>
  9. </receiver>

在清单文件中,该类需要meta-data

name为:android.appwidget.provider

resource为:在res/xml下定义的一个xml文件

3.创建清单文件中的resource文件

在res/xml下创建一个根节点为appwidget-provider的XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:initialLayout="@layout/appwidget_view"
  4. android:minWidth="250dp"
  5. android:minHeight="40dp"
  6. android:previewImage="@drawable/ic_launcher"
  7. android:updatePeriodMillis="1800000">
  8. </appwidget-provider>

initialLayout:Widget的布局文件,确定Widget中有哪些组件,怎么排列

minWidth:最小宽度    计算公式为:(70XN)-30,N为打算在屏幕上占几格,高度也是这么计算的

minHeight:最小高度

previewImage:选择部件时 展示的图像

updatePeriodMillis:更新时间间隔,单位毫秒。系统为了省电,默认是30分钟更新一次,如果你设置的值比30分钟小,系统也是30分钟才会更新一次。因为需要频繁更新的Widget,需要自己起一个service进行更新了。

只需要以上内容就可以使用Widget了,下面还有一些参数:

label: 选择部件时看到标签

icon: 选择部件时看到图标

resizeMode :调整size模式

configure: 如果需要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名

widgetCategory="keyguard|home_screen" widget:可添加的位置 锁屏界面|桌面

这样就可以完成一个Widget。在组件中就可以看到定义的Widget了,可以放在桌面程序上。

更新Widget需要用的AppWidgetManger类

以下为实现的代码和更新Widget的代码:

1.MyWidget.java

  1. public class MyWidget extends AppWidgetProvider {
  2.  
  3. //当把桌面上的Widget全部都删掉的时候,调用该方法
  4. @Override
  5. public void onDisabled(Context context) {
  6. super.onDisabled(context);
  7. Intent stopUpdateIntent = new Intent(context, UpdateWidgetService.class);
  8. context.stopService(stopUpdateIntent);
  9. }
  10.  
  11. //当Widget第一次创建的时候,该方法调用,然后启动后台的服务
  12. @Override
  13. public void onEnabled(Context context) {
  14. super.onEnabled(context);
  15. Intent startUpdateIntent = new Intent(context, UpdateWidgetService.class);
  16. context.startService(startUpdateIntent);
  17. }
  18.  
  19. @Override
  20. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  21. super.onUpdate(context, appWidgetManager, appWidgetIds);
  22. }
  23.  
  24. //在Widget使用中,会多次调用该方法
  25. @Override
  26. public void onReceive(Context context, Intent intent) {
  27. super.onReceive(context, intent);
  28. Intent startUpdateIntent = new Intent(context, UpdateWidgetService.class);
  29. context.startService(startUpdateIntent);
  30. }
  31. }

2.清单文件:

  1. //桌面widget
  2. <receiver android:name=".receiver.MyWidget">
  3. <intent-filter>
  4. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  5. </intent-filter>
  6.  
  7. <meta-data
  8. android:name="android.appwidget.provider"
  9. android:resource="@xml/myappwidget_info"/>
  10. </receiver>

3.myappwidget_info.xml,resource文件,定义在res/xml下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:initialLayout="@layout/appwidget_view"
  4. android:minWidth="294dp"
  5. android:minHeight="40dp"
  6. android:previewImage="@drawable/ic_launcher"
  7. android:updatePeriodMillis="1800000">
  8. </appwidget-provider>

4.appwidget_view.xml,Widget布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@drawable/widget_bg_portrait"
  6. android:gravity="center_vertical"
  7. android:orientation="horizontal">
  8.  
  9. <LinearLayout
  10. android:id="@+id/ll_processinfo"
  11. android:layout_width="0dp"
  12. android:layout_height="match_parent"
  13. android:layout_marginLeft="5dp"
  14. android:layout_weight="1"
  15. android:background="@drawable/widget_bg_portrait_child"
  16. android:gravity="center_vertical"
  17. android:orientation="vertical"
  18. android:paddingBottom="1dp"
  19. android:paddingTop="1dp">
  20.  
  21. <TextView
  22. android:id="@+id/tv_runprocessnumber"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginLeft="15dp"
  26. android:text="正在运行软件:14"
  27. android:textColor="@color/black"
  28. android:textSize="16sp"/>
  29.  
  30. <ImageView
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_marginBottom="1dp"
  34. android:layout_marginTop="1dp"
  35. android:src="@drawable/widget_bg_portrait_child_divider"/>
  36.  
  37. <TextView
  38. android:id="@+id/tv_avalimem"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_marginLeft="15dp"
  42. android:text="可用内存:300.52MB"
  43. android:textColor="@color/black"
  44. android:textSize="16sp"/>
  45.  
  46. </LinearLayout>
  47.  
  48. <LinearLayout
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:gravity="center_vertical"
  52. android:orientation="vertical">
  53.  
  54. <LinearLayout
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:gravity="center_vertical"
  58. android:orientation="horizontal"
  59. >
  60.  
  61. <ImageView
  62. android:layout_width="20dp"
  63. android:layout_height="20dp"
  64. android:src="@drawable/ic_launcher"/>
  65.  
  66. <TextView
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:text="手机安全卫士"
  70. android:textColor="@color/white"/>
  71.  
  72. </LinearLayout>
  73.  
  74. <Button
  75. android:id="@+id/btn_killall"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_centerVertical="true"
  79. android:layout_marginTop="3dp"
  80. android:background="@drawable/function_greenbutton_selector"
  81. android:gravity="center_vertical"
  82. android:text="一键清理"
  83. android:textColor="@color/function_greenbutton_textcolor_selector"/>
  84.  
  85. </LinearLayout>
  86.  
  87. </LinearLayout>

5.UpdateWidgetService.java,后台更新Widget的服务

  1. public class UpdateWidgetService extends Service {
  2.  
  3. private Timer timer;
  4. private TimerTask task;
  5.  
  6. public UpdateWidgetService() {
  7. }
  8.  
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. throw new UnsupportedOperationException("Not yet implemented");
  12. }
  13.  
  14. @Override
  15. public void onCreate() {
  16. timer = new Timer();
  17. task = new TimerTask() {
  18. @Override
  19. public void run() {
  20. int runningTaskCount = SystemInfoUtils.getRunningTaskCount(UpdateWidgetService.this);
  21. long avaliMem = SystemInfoUtils.getAvaliMem(UpdateWidgetService.this);
  22. ComponentName componentName = new ComponentName(UpdateWidgetService.this, MyWidget.class);
  23. RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.appwidget_view);
  24.  
  25. //设置Widget中Textview的显示内容
  26. remoteViews.setTextViewText(R.id.tv_runprocessnumber, "正在运行软件:" + runningTaskCount);
  27. remoteViews.setTextViewText(R.id.tv_avalimem, "可用内存:" + Formatter.formatFileSize(UpdateWidgetService.this, avaliMem));
  28.  
  29. //点击widget的一键清理按钮,发送广播,在AutoKillTaskReceiver广播中杀掉所有的进程。
  30. Intent intent = new Intent(UpdateWidgetService.this, AutoKillTaskReceiver.class);
  31. PendingIntent pendingIntent = PendingIntent.getBroadcast(UpdateWidgetService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  32. remoteViews.setOnClickPendingIntent(R.id.btn_killall, pendingIntent);
  33.  
  34. //点击widget显示信息部分,跳到程序管理页面
  35. Intent startActivityIntent = new Intent(UpdateWidgetService.this, TaskManagerActivity.class);
  36. startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  37. PendingIntent processInfoIntent = PendingIntent.getActivity(UpdateWidgetService.this, 0, startActivityIntent, PendingIntent.FLAG_ONE_SHOT);
  38. remoteViews.setOnClickPendingIntent(R.id.ll_processinfo, processInfoIntent);
  39.  
  40. //由AppWidgetManager处理Wiget。
  41. AppWidgetManager awm = AppWidgetManager.getInstance(getApplicationContext());
  42. awm.updateAppWidget(componentName, remoteViews);
  43.  
  44. }
  45. };
  46. timer.schedule(task, 0, 3000);
  47. super.onCreate();
  48. }
  49.  
  50. @Override
  51. public void onDestroy() {
  52. super.onDestroy();
  53. timer.cancel();
  54. task.cancel();
  55. timer = null;
  56. task = null;
  57. }
  58. }

6.实现效果图:

    

Android开发之创建App Widget和更新Widget内容的更多相关文章

  1. 基于Android开发的天气预报app(源码下载)

    原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...

  2. Android开发2——创建测试项目

    一.创建普通Android项目   二.在AndroidManifest.xml添加两个配置 <?xml version="1.0" encoding="utf-8 ...

  3. Android开发走过的坑(持续更新)

    1 华为 nova真机 打印不出Log 参考资料:http://www.apkbus.com/thread-585228-1-1.html 解决:针对权限问题,我们当然也可以解决的,华为手机在你的拨号 ...

  4. Android开发 ---代码创建选项菜单、隐藏菜单项、菜单的生命周期,菜单按钮图标设置、搜索框、xml中设置子菜单

    1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> < ...

  5. android 开发 实现一个app的引导查看页面(使用ViewPager组件)

    我们安装完app后第一次打开app,通常都会有一个翻页图片形式的app引导简介说明.下面我们来实现这个功能.ViewPager这个组件与ListView和RecyclerView在使用上有很高的相似处 ...

  6. 【Android开发】创建你的第一个Android项目

    原文:http://android.eoe.cn/topic/summary 本文中你将了解到: 1. 使用Eclipse创建项目 2. 使用命令行创建项目 你还应该阅读: 1. 安装SDK(官网页面 ...

  7. (转)Android开发出来的APP在手机的安装路径是?

    一.安装路径在哪? Android应用安装涉及到如下几个目录: system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.dat ...

  8. android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)

    基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...

  9. 入门移动端混合开发 实战京东 APP(完整更新)

    课程资料获取链接:点击这里 混合开发入门 主流开发方案实战京东移动端APP 无需原生开发基础,也能完美呈现京东商城.本课程融合vue.Android.IOS等目前流行的前端和移动端技术,混合开发经典电 ...

随机推荐

  1. 执行umount 命令的时候出现 device is busy

    执行umount 命令的时候出现 device is busy ,有人在使用这块磁盘 umount /dev/sde1 umount: /u01/app/oracle: device is busy ...

  2. (菜鸟要飞系列)四,基于Asp.Net MVC5的后台管理系统(zTree绑定Json数据生成树)

    上一次老师让我们用递归将中国城市镇县四级联动 显示在树上,那个时候就知道可以显示在zTree上,可是苦于对Json的不了解,对zTree的Api的不了解,一直没有做出来,只好将递归算法显示在了窗体上, ...

  3. C# 清楚Cookies

    //销毁Cookies中的数据 if (Request.Cookies["Ticket"] != null) { HttpCookie mycookie; mycookie = R ...

  4. 【个人笔记】001-PHP基础-01-PHP快速入门-01-PHP职业路线及PHP前景

    001-PHP基础-01-PHP快速入门 01-PHP职业路线及PHP前景 PHP职业路线 PHP初级工程师 1年以下 3k-6k PHP中级工程师 1-3年6k-10k PHP高级工程师 3年以上  ...

  5. java使用.net的webservice

    1.下载最新的axis2 http://mirrors.hust.edu.cn/apache//axis/axis2/java/core/1.6.3/axis2-1.6.3-bin.zip 2.解压使 ...

  6. 编写一个递归函数,输出vector对象的内容

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  7. .Net 使用 Oracle 提供组件访问数据库

    向导式安装客户端组件 32位下载: http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html   批处理式安装客户端组件 ...

  8. MySQL 5.7原生JSON格式支持

    在MySQL与PostgreSQL的对比中,PG的JSON格式支持优势总是不断被拿来比较.其实早先MariaDB也有对非结构化的数据进行存储的方案,称为dynamic column,但是方案是通过BL ...

  9. 使用shell脚本获取虚拟机中cpu使用率(读/proc/statc)

    #!/bin/bash interval= cpu_num=`-] -c` start_idle=() start_total=() cpu_rate=() cpu_rate_file=./`host ...

  10. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...