android之appwidget(一)简单appwidget
2019独角兽企业重金招聘Python工程师标准>>>
什么是appwidget? 即application widget。 应用程序控件,就是在桌面显示的控件。 AppWidget就是我们平常在桌面上见到的那种一个个的小控件,利用这个小控件可以给用户提供一些方便快捷的操作。
本次创建一个简单的appwidget。
相关概念:
1、AppWidgetProviderInfo对象:它是一个xml文件,为appWidget提供元数据,包括布局,更新频率等数据。这个对象定义在xml中。
2、AppWidgetProvider: 定义了appwidget的基本生命周期函数。
不多说,一切尽在代码:
1、在layout中新建一个appwidgetlayout.xml文件。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/textview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextView android:id="@+id/textview"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>
</LinearLayout
没什么多说的,只是一个textview。这个是我们的应用程序在桌面的控件。
2、在res中新建一个xml包,在这个包中新建一个appwidget_info.xml文件。
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="294dp"
- android:minHeight="72dp"
- android:updatePeriodMillis="86400000"
- android:initialLayout="@layout/appwidgetlayout"
- />
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="294dp"android:minHeight="72dp"android:updatePeriodMillis="86400000"android:initialLayout="@layout/appwidgetlayout"/>
这个xml为appwidget提供元数据,这个是在桌面的布局,而这个layout(appwidgetlayout)为在appwidget中控件的布局。
3、在包下新建我们的provider,他继承了AppWidgetProvider,我们只需要对它的生命周期进行处理就行了。
- public class AppWidgetprovider extends AppWidgetProvider {
- @Override
- public void onDeleted(Context context, int[] appWidgetIds) {
- // TODO Auto-generated method stub
- System.out.println("onDeleted");
- super.onDeleted(context, appWidgetIds);
- }
- @Override
- public void onDisabled(Context context) {
- // TODO Auto-generated method stub
- System.out.println("onDisable");
- super.onDisabled(context);
- }
- @Override
- public void onEnabled(Context context) {
- // TODO Auto-generated method stub
- System.out.println("onEnable");
- super.onEnabled(context);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- System.out.println("onreceive");
- super.onReceive(context, intent);
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- // TODO Auto-generated method stub
- System.out.println("onUpdate");
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
- }
public class AppWidgetprovider extends AppWidgetProvider {@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {// TODO Auto-generated method stubSystem.out.println("onDeleted");super.onDeleted(context, appWidgetIds);}@Overridepublic void onDisabled(Context context) {// TODO Auto-generated method stubSystem.out.println("onDisable");super.onDisabled(context);}@Overridepublic void onEnabled(Context context) {// TODO Auto-generated method stubSystem.out.println("onEnable");super.onEnabled(context);}@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubSystem.out.println("onreceive");super.onReceive(context, intent);}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {// TODO Auto-generated method stubSystem.out.println("onUpdate");super.onUpdate(context, appWidgetManager, appWidgetIds);}}
4、在AndroidManifest.xml中进行配置,由于这个appwidget使用的是广播机制,我们需要在AndroidManifest.xml中进行如下配置:
- <receiver android:name="AppWidgetprovider">
- <intent-filter >
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
- </intent-filter>
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/appwidget_info"
- />
- </receiver>
<receiver android:name="AppWidgetprovider"><intent-filter ><action android:name="android.appwidget.action.APPWIDGET_UPDATE"/></intent-filter><meta-data android:name="android.appwidget.provider"android:resource="@xml/appwidget_info"/></receiver>
通过过滤器我们截取APPWIDGET_UPDATE这个action,然后对provider进行资源配置,对应的刚刚那个xml中的文件。
转载于:https://my.oschina.net/u/246578/blog/71840
android之appwidget(一)简单appwidget相关推荐
- 实现一个用于显示当前时间的Google Android 窗口小部件(AppWidget)
Android不用我多说了,这是Google在2007年推出的一款主要用于移动设备的操作系统.google在android sdk1.5中加入了appwidget api.通过这项技术,可以在a ...
- Android TabLayout(选项卡布局)简单用法实例分析
本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到 ...
- android 共享数据,android进程间共享简单数据
我们知道,在android中,保存简单的数据最方便的就是使用SharedPreferences,然而,SharedPreferences虽然说也可以设置成进程间共享数据,但是并不可靠(更致命的是,不同 ...
- Android PC投屏简单尝试—最终章2
源码地址:https://github.com/deepsadness/AppRemote 上一章中,我们简单实现了PC的投屏功能. 但是还是存在这一些缺陷. 屏幕的尺寸数据是写死的 不能通过PC来对 ...
- Android PC投屏简单尝试—最终章1
回顾之前的几遍文章,我们分别通过RMTP协议和简单的Socket 发送Bitmap图片的Base64编码来完成投屏. 回想这系列文章的想法来源-Vysor,它通过 USB来进行连接的.又看到了 scr ...
- Android PC投屏简单尝试(录屏直播)2—硬解章(MediaCodec+RMTP)
代码地址 :https://github.com/deepsadness/MediaProjectionDemo 想法来源 上一边文章的最后说使用录制的Api进行录屏直播.本来这边文章是预计在5月份完 ...
- Android PC投屏简单尝试- 自定义协议章(Socket+Bitmap)
代码地址 :https://github.com/deepsadness/MediaProjectionDemo 效果预览 投屏效果预览 简单说明: 使用Android MediaProjection ...
- Xamarin.Android之封装个简单的网络请求类
http://doc.okbase.net/catcher1994/archive/220195.html Catcher8 2016/4/23 0:28:50 阅读(72) 评论(0) 一.前言 回 ...
- Android 6.0 超级简单的权限申请2 (Permission)
代码地址如下: http://www.demodashi.com/demo/13506.html 写在前面 上次写了一个权限申请的例子Android 6.0 超级简单的权限申请 (Permission ...
- 安卓逆向_22( 一 ) --- Xposed【 Android Studio + Xposed 实现简单的 hook 】
From:使用渗透测试框架 Xposed 框架 hook 调试 Android APP:https://www.freebuf.com/articles/terminal/56453.html Xpo ...
最新文章
- 我画了35张图就是为了让你深入 AQS
- 关于如何开办餐饮店?
- Spring Boot构建多租户SaaS平台核心技术指南
- 修改hostname
- 整人之bat重启文件
- mysql统计各部门人数_2019年内蒙古普通高校招生考试各分数段人数统计表公布
- vue 分模块打包 脚手架_Vue面试官最爱的底层源码问题,你可以这样回答!
- smart gesture安装失败_WinCC flexible SMART V3 SP2安装步骤以及常见错误解决方法
- bulk of the 用法_高考英语|10大词类用法考点详解
- 社会内卷的真正原因:华为内部论坛的这篇短文讲透了
- Python实战从入门到精通第十七讲——将单方法的类转换为函数
- Python 获取对象信息 —— dype 与 dir
- 985 硕士待业200天,工作 10 年存款 2W : 累死你的不是工作,而是“选择”
- ElasticNet算法解析
- h5 右下角浮动按钮_Flutter 浮动按钮-FloatingActionButton的使用
- Selenium 爬取微信公众号文章并保存为pdf与长图片(2022-06-09更新版)
- 西门子1200plc与施耐德变频器modbus通讯程序
- js版in_array函数
- Linux杀毒软件之ClamAV
- Oracle日期转换成格林威治时间
热门文章
- poj 2723 2-SAT问题
- Lua中的操作系统库
- transferto遇到的问题java.io.FileNotFoundException: C:\Users\Administrator\AppData\Local\Temp
- Kafka产品迭代计划(RoadMap)
- WebService之Java原生态支持
- 浅谈浏览器多进程与JS线程 1
- Java通过引用操作对象的“共享”特性
- AOP五大通知注解详解
- listdir在python3_Python3 os.listdir() 方法
- [树状数组][哈希]JZOJ 3240 Seat