- 浏览: 256891 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
sunshine_bean:
第四行改进下URL=`svn info |grep &quo ...
linux判断是否需要svn up的脚本 -
leokelly001:
设置请求头,user-agent就行了
android使用豆瓣API出现500错误及解决方法 -
貌似掉线:
txy7121 写道HandlerFactory和AntiCo ...
大谈android安全2——Activity劫持的防范程序 -
txy7121:
HandlerFactory和AntiConstants这两个 ...
大谈android安全2——Activity劫持的防范程序 -
貌似掉线:
hyc_willie 写道关注着你的框架,希望能见到它的发布 ...
androidkit——Android开发框架
在android平台中,显示在HOME界面的一些挂件,即桌面小部件,被称为AppWidget。在自己的程序中适当地加入AppWidget,不但使用户更方便,也能从一定程序上提高本程序的留存率。
下面通过我所写的一个课表应用来说明如何使用AppWidget。
我所写的AppWidget最终结果如下图:
1.首先在res/layout下编写AppWidget的布局文件。
我的代码如下:
appwidget_small.xml
其中include的是课表信息部分的布局,它在我的MainActivity还用到,这里没有另外编写,直接使用include标签将它引用进来。
main_list_item.xml的代码如下:
在这里说明一下,对于appWidget的布局文件的根标签如果设置宽高为match_parent(fill_partent),则在HOME界面改变它的大小时它也会自动扩张,否则无论将它占的空间拉伸到多大,它都不会扩张。
2.在/res/xml下编写这个appwidget的信息文件。
widget_small_provider_info.xml代码如下:
上面initialLayout即初始的布局,minHeight和minWidth即最小的宽高,updatePeriodMillis为更新周期。
需要注意的是关于这个更新周期,在我本机G14,android4.0.3上发现它并没有用,百度之后发现它存在着BUG,在有些系统有效,有些则没效。所以如果想在任何机型都能自动更新的话,还要自己写一个service去更新。这个可参考android SDK中的例子。
3.接下来需要编写一个类,继承自AppWidgetProvider。
代码如下:
上面onEnabled在第一次创建AppWidget中执行。
在HOME界面中是可以多次插入同一个AppWidget的,每次插入一个AppWidget,onReceive和onUpdate都会被执行,这一次可以自己去做试验了解它的生命周期,在这里不赘述。
4.在Manifest中声明。
在上面的例子中,一个简单的AppWidget就完成了。
但是我需要的还不够,我还希望这个AppWidget的控件内容是可以改变的,而不是写死在布局文件中的,这就需要用到RemoteViews了。
因为在android中,AppWidget与你的主程序是运行在不同的进程当中的,在这里需要用RemoteViews来进行它们之间的通信,而不是像在Activity中那样方便。
而对于RemoteViews在不同版本的API中,支持的控件(亦其提供的方法)也不同,越往后支持的越多,在2.2中,貌似还不支持AbsListView控件的更新,也只提供了简单的onclick事件绑定的方法。
首先创建一个RemoteViwews对象,RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_small);
然后可以通过它的setTextViewText方法设置AppWidget中的TextView的内容,传入的参数为要设置的textview的id和内容。
如果想点击它而打开你的程序的activity,或者更新控件,则也如下面代码所示。
在更新控件中,由于它是发送了一个广播,onReceive将会被执行,但不会执行onUpdate方法,所以这里还需要再修改onReceive方法,否则无法达到更新的目的。
下面通过我所写的一个课表应用来说明如何使用AppWidget。
我所写的AppWidget最终结果如下图:
1.首先在res/layout下编写AppWidget的布局文件。
我的代码如下:
appwidget_small.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/appwidget_bg" > <Button android:id="@+id/widget_small_refresh" android:layout_width="60dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:text="@string/widget_small_refrest" android:textAppearance="@android:style/TextAppearance.Medium" /> <TextView android:id="@+id/widget_small_day" android:layout_width="60dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_below="@id/widget_small_refresh" android:gravity="center" android:textAppearance="@android:style/TextAppearance.Small" /> <include android:id="@+id/widget_small_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="2dp" android:layout_marginLeft="2dp" android:layout_marginTop="2dp" android:layout_toLeftOf="@id/widget_small_refresh" layout="@layout/main_list_item" android:background="@drawable/appwidget_list_bg" /> </RelativeLayout>
其中include的是课表信息部分的布局,它在我的MainActivity还用到,这里没有另外编写,直接使用include标签将它引用进来。
main_list_item.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/list_item_class" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" /> <TextView android:id="@+id/list_item_time" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Small" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="120dp" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="vertical" > <TextView android:id="@+id/list_item_course" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" /> <TextView android:id="@+id/list_item_teacher" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" /> </LinearLayout> <LinearLayout android:layout_width="120dp" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="vertical" > <TextView android:id="@+id/list_item_room" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" /> <TextView android:id="@+id/list_item_week" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" android:textAppearance="@android:style/TextAppearance.Medium" /> </LinearLayout> </LinearLayout> </LinearLayout>
在这里说明一下,对于appWidget的布局文件的根标签如果设置宽高为match_parent(fill_partent),则在HOME界面改变它的大小时它也会自动扩张,否则无论将它占的空间拉伸到多大,它都不会扩张。
2.在/res/xml下编写这个appwidget的信息文件。
widget_small_provider_info.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/appwidget_small" android:minHeight="60dp" android:minWidth="240dp" android:updatePeriodMillis="1800000" > </appwidget-provider>
上面initialLayout即初始的布局,minHeight和minWidth即最小的宽高,updatePeriodMillis为更新周期。
需要注意的是关于这个更新周期,在我本机G14,android4.0.3上发现它并没有用,百度之后发现它存在着BUG,在有些系统有效,有些则没效。所以如果想在任何机型都能自动更新的话,还要自己写一个service去更新。这个可参考android SDK中的例子。
3.接下来需要编写一个类,继承自AppWidgetProvider。
代码如下:
/* * @(#)TableWidgetProvider.java Project:UniversityTimetable * Date:2013-2-11 * * Copyright (c) 2013 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lurencun.cfuture09.universityTimetable.appwidget; import java.util.Calendar; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.util.Log; import android.widget.RemoteViews; import com.lurencun.cfuture09.universityTimetable.R; /** * @Author Geek_Soledad (66704238@51uc.com) * @Function */ public class TableSmallWidgetProvider extends AppWidgetProvider { private static final String TAG = "TableSmallWidgetProvider"; public static final String ACTION_UPDATE = "cfuture09.universityTimetable.action.TIMETABLE.APPWIDGET_SMALL_UPDATE"; @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); Log.d(TAG, "onDeleted"); } @Override public void onDisabled(Context context) { super.onDisabled(context); Log.d(TAG, "onDisable"); } @Override public void onEnabled(Context context) { super.onEnabled(context); Log.d(TAG, "onEnabled"); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); Log.d(TAG, "onReceive"); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.d(TAG, "update"); } }
上面onEnabled在第一次创建AppWidget中执行。
在HOME界面中是可以多次插入同一个AppWidget的,每次插入一个AppWidget,onReceive和onUpdate都会被执行,这一次可以自己去做试验了解它的生命周期,在这里不赘述。
4.在Manifest中声明。
<receiver android:name=".appwidget.TableSmallWidgetProvider" android:label="@string/widget_small_4_1" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="cfuture09.universityTimetable.action.TIMETABLE.APPWIDGET_SMALL_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_small_provider_info" /> </receiver>其中label中引用的字符串即在插入Appwidget时出现的那个名字,如这里为"大学课程表(4*1)",如果不添加,默认为你的程序名,即在application标签中声明的label。然后加入的intent-filter,为其接收的广播,这里还定义了自己的一个action,它将在下面的例子中用到,因为我希望还能手动更新appwidget的数据。
在上面的例子中,一个简单的AppWidget就完成了。
但是我需要的还不够,我还希望这个AppWidget的控件内容是可以改变的,而不是写死在布局文件中的,这就需要用到RemoteViews了。
因为在android中,AppWidget与你的主程序是运行在不同的进程当中的,在这里需要用RemoteViews来进行它们之间的通信,而不是像在Activity中那样方便。
而对于RemoteViews在不同版本的API中,支持的控件(亦其提供的方法)也不同,越往后支持的越多,在2.2中,貌似还不支持AbsListView控件的更新,也只提供了简单的onclick事件绑定的方法。
首先创建一个RemoteViwews对象,RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_small);
然后可以通过它的setTextViewText方法设置AppWidget中的TextView的内容,传入的参数为要设置的textview的id和内容。
如果想点击它而打开你的程序的activity,或者更新控件,则也如下面代码所示。
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.d(TAG, "onUpdate"); for (int i = 0; i < appWidgetIds.length; i++) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_small); // 设置星期几 remoteViews.setTextViewText(R.id.widget_small_day, arrayWeeks[currentDay]); // 打开程序 PendingIntent startIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); remoteViews.setOnClickPendingIntent(R.id.widget_small_content, startIntent); // 更新控件的事件绑定 Intent intent = new Intent(); intent.setAction(ACTION_UPDATE); // 以发送广播消息的方式创建PendingIntent. PendingIntent pending_intent = PendingIntent.getBroadcast(context, 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.widget_small_refresh, pending_intent); appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } }
在更新控件中,由于它是发送了一个广播,onReceive将会被执行,但不会执行onUpdate方法,所以这里还需要再修改onReceive方法,否则无法达到更新的目的。
@Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive"); if (ACTION_UPDATE.equals(intent.getAction())) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_small); // 设置星期几 remoteViews.setTextViewText(R.id.widget_small_day, arrayWeeks[currentDay]); // 更新节课 updateWidgetViews(remoteViews, dto); PendingIntent startIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); remoteViews.setOnClickPendingIntent(R.id.widget_small_content, startIntent); ComponentName componentName = new ComponentName(context, TableSmallWidgetProvider.class); AppWidgetManager.getInstance(context).updateAppWidget( componentName, remoteViews); } else { super.onReceive(context, intent); } }
发表评论
-
利用电脑玩Android版“天天连萌”刷高分(四)——模拟按键及程序优化
2014-01-22 00:14 1664这一系列文章,没想到从去年10月份以来,写了三篇我就忘了写了, ... -
利用电脑玩Android版“天天连萌”刷高分(三)——连连看消除搜索
2013-10-29 17:02 1145差点忘了写接下来的这两篇博客了,这篇如果接不上上一篇,请勿见怪 ... -
利用电脑玩Android版“天天连萌”刷高分(二)——图像识别
2013-10-20 15:44 1489上一篇只是提到了在PC端利用android sdk里面的工具进 ... -
利用电脑玩Android版“天天连萌”刷高分(一)——截图
2013-10-20 11:22 3376这几周微信游戏“天天 ... -
在PC端进行android截屏的多种方法
2013-10-18 11:01 3537昨晚意外的发现在PC端进行截屏的方法相当多,在android ... -
近期计划
2013-09-01 20:02 1193在上一篇(http://maosidiao ... -
Android开源游戏引擎之Angle(二)——开始前的准备
2013-09-01 12:04 2427在上一篇博客(http://mao ... -
Android开源游戏引擎之Angle(一)——概述
2013-08-31 19:26 5966Angle是一个Android平台上 ... -
设置tabWidget标题的字体大小及颜色
2013-03-27 13:47 9396/* * @(#)TextAppearenceUtil ... -
android使用actionbar与fragment
2013-01-16 23:12 2256android使用actionbar中的tab,及fragme ... -
android使用豆瓣API出现500错误及解决方法
2013-01-10 18:56 6184为团队做一个图书管理的应用,涉及到扫描ISBN然后查询图书信息 ... -
进入程序的动画IntroActivity增强版
2012-12-11 10:19 1439这是对上一次谈到的android程序进入前的动画的封装(详见: ... -
androidkit发布0.5.3alpha版
2012-12-07 09:26 1194androidkit是一个用于android应用层开发的工具包 ... -
使用Zxing及豆瓣API
2012-12-06 20:30 2362本文原创,转载请注明原文地址:http://maosidiao ... -
androidkit——减少android开发代码的工具包
2012-11-16 22:25 2190androidkit是我学android开发以来,慢慢在封装的 ... -
android开发知识点1——按钮效果
2012-10-30 15:15 2293android中控件的背景或ImageView、ImageBu ... -
android颜色选择器
2012-09-24 13:39 3310前段时间想到要封装一个颜色选择器的类,查询后知道谷歌api d ... -
androidt程序文字锯齿
2012-09-24 00:51 1252刚才才发现,在开发android程序时,如果在manifest ... -
大谈android安全2——Activity劫持的防范程序
2012-09-23 23:13 9305本文原创,转载请注明出处: http://maosidiaox ... -
android程序开启动画封装
2012-09-20 22:24 5719本文及代码原创,转载请注明出处:http://maosidia ...
相关推荐
AppWidget 即桌面小部件,也叫桌面控件,就是能直接显示在Android系统桌面上的小程序,先看图: 图中我用黄色箭头指示的即为AppWidget,一些用户使用比较频繁的程序,可以做成AppWidget,这样能方便地使用。典型...
android 桌面组件 App widget的使用.
Android高级应用源码-Android小部件AppWidget.zip
NULL 博文链接:https://hanllove001.iteye.com/blog/1185128
Android 桌面小部件(widget)日签 ,展示农历阳历日期,一年进度,定时随机诗句
一个最简单的桌面小组件开发实例。点击之后发送广播消息,接收到消息之后更新小组件上的文字。是学习AppWidge类的好例子。
AppWidget是放置在手机屏幕的桌面小组件应用,如时钟、日历、天气等组件,与一般应用程序有所不同。一般应用虽也可以以图标的形式(快捷方式)放在桌面,但必须点击运行和查看;而AppWidget一般不须点击即直观呈现其...
本文实例讲述了Android桌面组件App Widget用法。分享给大家供大家参考。具体如下: 这里模拟一个案例:把AppWidget添加到桌面后,点击AppWidget后AppWidget文本会轮回改变 main.xml布局文件: <?xml version=1.0...
本文实例讲述了Android桌面组件App Widget用法。分享给大家供大家参考。具体如下: Android开发应用除了程序应用,还有App Widget应用。好多人会开发程序应用而不会开发App Widget应用。本帖子就是帮助大家学习如何...
NULL 博文链接:https://l12052124.iteye.com/blog/758380
NULL 博文链接:https://676744379-qq-com.iteye.com/blog/1125305
可将你的心情讯录并用桌面组件的形式展示于桌面上,并有丰富的表情可供选择并加载在桌面上,此功能类似于QQ上的各性签名,可以看到手机主人的心情状况不是很好,注:因为这个小程序完全是App widget 桌面组件,所以...
桌面小部件使用案例,实现AppWidgets主要功能类AppWidgetProvider生命周期解析 AppWidgetProvider生命周期 1、在桌面上添加小部件,让小部件可用会调用:onEnabled 2、作为通知会调用onReceive 3、小部件可用后会...
自己写的百度搜索的appwidget,借鉴模仿豌豆荚、谷歌搜索等桌面部件