- 浏览: 545299 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
q649916440:
使用这个的目的是什么呢?感觉没啥优势啊,用起来还绕个大圈
使用googleapi-client-java操作gtasks(一) -
文艺吧网:
还有一个GZIP的问题,我怎么转都乱码最后是因为要解压一下ht ...
关于使用InputStreamReader读取GBK编码文件乱码的问题 -
xiaodousa:
9楼正解!
Android在Listview中使用EditText -
fxiaozj:
zyp09 写道很想知道在Mainactivity界面怎么获得 ...
Android PreferenceActivity 学习笔记 -
zylc369:
楼主很用心,一定要顶
Android通过共享用户ID来实现多Activity进程共享
1.创建AppWidget布局,包含两个TextView用来显示内容:
2.在res下创建xml目录,再在xml目录里面创建AppWidget信息xml文件:
2.1新建xml文件时,type选择AppWidget Provider。
2.2填充属性:
宽高的计算公式为:占用屏幕格数*74-2
Update period millis:设置为0,手动刷新。根据实验,设置不为0时,至少在2.2上系统根本不按照设置的值刷新,还是自己控制刷新时机好了。
Initial layout:就添控件要使用的布局。
Configure暂时不用,留空。
3.创建AppWidgetDemo类:
重载AppWidgetProvider中的所有函数,每个函数里面增加输出语句,以查看调用顺序。
4.在AndroidManifest.xml文件中声明此Widget:
添加一个Receiver,其name为AppWidgetDemo类的类名。
为此Receiver添加Intent filter,接收系统发出android.appwidget.action.APPWIDGET_UPDATE的Intent。
此外还要为此receiver添加meta-data信息,以告知系统相关的AppWidgetProvider信息:
meta-data的name是约定好的android.appwidget.provider,resource则是第2步创建的AppWidget信息xml文件。
5.至此AppWidget已经可用了,安装到模拟器上看下运行流程。
5.1添加一个Widget到桌面上:
onEnabled被呼叫:按照说明,当桌面上出现第一个此Widget的实例时,此函数被呼叫。
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_ENABLED
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量为1
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE
5.2再添加一个Widget到桌面上:
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量仍然为1,而不是2。
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE
5.3从桌面上移除一个Widget:
onDeleted:每个实例被移除时都会被呼叫
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED
5.4再从桌面上移除一个Widget:
onDeleted:仍然执行
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED
onDisabled:因为是最后一个活动的实例被移除了,所以被呼叫。
onReceive,Action:android.appwidget.action.APPWIDGET_DISABLED
6.刷新AppWidget
6.1在onUpdate()中刷新:
onUpdate在AppWidget放到桌面时会被调用,在Update period millis达到时可能会被调用。
先获取一个RemoteViews,也就是AppWidget的布局所对应的View;
使用指定的Id更新要更新的控件;
更新整个RemoteViews,此时就可以更新AppWidget内容了。
但是这样的代码还有一个问题,当向桌面依次添加多个控件时会出现下面这样的效果:
即更新时没有同时更新所有的AppWidget,这是因为onUpdate中传进来的数组中只包含了1个id,如果想同时更新多个,那么可以把更新语句更换为:
通过组件名可以把所有的名字符合的AppWidget都更新。
6.2在onReceive()中更新
6.2.1自定义Action通知刷新:
在AndroidManifest.xml中定义的receiver的intent-filter增加一个自定义的Action:
com.demo.appwidget.refresh
包含此Action的Intent可以在后台服务或者Activity中发出,然后会被此Receiver接收,进而触发onReceive。
本例中采用按钮按下时广播intent:
在接收端:
判断是否是感兴趣的Action,是的话就取值,然后更新。
6.2.2接收系统消息刷新:
比如intent-filter中再增加一个action:"android.provider.Telephony.SMS_RECEIVED",在AndroidMenifest.xml中任意位置添加<uses-permission android:name="android.permission.RECEIVE_SMS" />设置好权限,当系统有短消息收到时就能触发onReceive了。
但是有些Action比较特殊,比如android.intent.action.TIME_TICK,根据android.content.intent文档中的描述:You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().
这个Action在AndroidManifest.xml中声明了也没用,必须要自己开个服务注册receiver才能收到,然后再转发一次给自己。
6.3直接在外部Activity或者Service中刷新:
此段代码可直接刷新AppWidget的内容,不会触发其onUpdate()。
7.响应点击事件
因为onUpdate是每个AppWidget被放置到桌面上时都会被呼叫的函数,所以在此函数中完成事件的关联:
另外要注意此段代码必须要在appWidgetManager.updateAppWidget()之前,否则是不会生效的。
运行后可以点击AppWidget的第一个控件,就能呼叫指定的Activity了。
8.Config Activity
这个可直接参考SDK文档中的Dev Guide-->App Widgets了。
<?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="fill_parent" android:orientation="vertical"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tv1" android:textColor="#FF0000" android:textSize="24sp" android:textStyle="bold" android:text="-1"></TextView> <TextView android:layout_height="wrap_content" android:id="@+id/tv2" android:textSize="24sp" android:textStyle="bold" android:textColor="#00FF00" android:layout_width="fill_parent" android:text="-2"></TextView> </LinearLayout>
2.在res下创建xml目录,再在xml目录里面创建AppWidget信息xml文件:
2.1新建xml文件时,type选择AppWidget Provider。
2.2填充属性:
宽高的计算公式为:占用屏幕格数*74-2
Update period millis:设置为0,手动刷新。根据实验,设置不为0时,至少在2.2上系统根本不按照设置的值刷新,还是自己控制刷新时机好了。
Initial layout:就添控件要使用的布局。
Configure暂时不用,留空。
3.创建AppWidgetDemo类:
重载AppWidgetProvider中的所有函数,每个函数里面增加输出语句,以查看调用顺序。
public class AppWidgetDemo extends AppWidgetProvider { @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); Log.e("AppWidgetDemo", "onDeleted"); } @Override public void onDisabled(Context context) { // TODO Auto-generated method stub super.onDisabled(context); Log.e("AppWidgetDemo", "onDisabled"); } @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); Log.e("AppWidgetDemo", "onEnabled"); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); Log.e("AppWidgetDemo", "onReceive,Action:" + intent.getAction()); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); Log.e("AppWidgetDemo", "onUpdate,Count:" + appWidgetIds.length); } }
4.在AndroidManifest.xml文件中声明此Widget:
添加一个Receiver,其name为AppWidgetDemo类的类名。
<receiver android:name="AppWidgetDemo"></receiver>
为此Receiver添加Intent filter,接收系统发出android.appwidget.action.APPWIDGET_UPDATE的Intent。
<receiver android:name="AppWidgetDemo"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> </receiver>
此外还要为此receiver添加meta-data信息,以告知系统相关的AppWidgetProvider信息:
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info_demo"></meta-data>
meta-data的name是约定好的android.appwidget.provider,resource则是第2步创建的AppWidget信息xml文件。
5.至此AppWidget已经可用了,安装到模拟器上看下运行流程。
5.1添加一个Widget到桌面上:
onEnabled被呼叫:按照说明,当桌面上出现第一个此Widget的实例时,此函数被呼叫。
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_ENABLED
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量为1
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE
5.2再添加一个Widget到桌面上:
onUpdate被呼叫:onUpdate,Count:1,并且待更新的AppWidget数量仍然为1,而不是2。
onReceive被呼叫:onReceive,Action:android.appwidget.action.APPWIDGET_UPDATE
5.3从桌面上移除一个Widget:
onDeleted:每个实例被移除时都会被呼叫
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED
5.4再从桌面上移除一个Widget:
onDeleted:仍然执行
onReceive,Action:android.appwidget.action.APPWIDGET_DELETED
onDisabled:因为是最后一个活动的实例被移除了,所以被呼叫。
onReceive,Action:android.appwidget.action.APPWIDGET_DISABLED
6.刷新AppWidget
6.1在onUpdate()中刷新:
onUpdate在AppWidget放到桌面时会被调用,在Update period millis达到时可能会被调用。
RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.widget_layout_demo); appWidgetView.setTextViewText(R.id.tv1, String.valueOf(mCount)); appWidgetView.setTextViewText(R.id.tv2, String.valueOf(mCount)); appWidgetManager.updateAppWidget(appWidgetIds, appWidgetView);
先获取一个RemoteViews,也就是AppWidget的布局所对应的View;
使用指定的Id更新要更新的控件;
更新整个RemoteViews,此时就可以更新AppWidget内容了。
但是这样的代码还有一个问题,当向桌面依次添加多个控件时会出现下面这样的效果:
即更新时没有同时更新所有的AppWidget,这是因为onUpdate中传进来的数组中只包含了1个id,如果想同时更新多个,那么可以把更新语句更换为:
appWidgetManager.updateAppWidget(new ComponentName(context, AppWidgetDemo.class), appWidgetView);
通过组件名可以把所有的名字符合的AppWidget都更新。
6.2在onReceive()中更新
6.2.1自定义Action通知刷新:
在AndroidManifest.xml中定义的receiver的intent-filter增加一个自定义的Action:
com.demo.appwidget.refresh
<receiver android:name="AppWidgetDemo"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> <action android:name="com.demo.appwidget.refresh"></action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info_demo"></meta-data> </receiver>
包含此Action的Intent可以在后台服务或者Activity中发出,然后会被此Receiver接收,进而触发onReceive。
本例中采用按钮按下时广播intent:
btnSend.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.demo.appwidget.refresh"); intent.putExtra("value", teInput.getText().toString()); SendMsgActivity.this.sendBroadcast(intent); } });
在接收端:
if(intent.getAction().equals("com.demo.appwidget.refresh")) { String value = intent.getStringExtra("value"); RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.widget_layout_demo); appWidgetView.setTextViewText(R.id.tv1, value); appWidgetView.setTextViewText(R.id.tv2, value); AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, AppWidgetDemo.class), appWidgetView); }
判断是否是感兴趣的Action,是的话就取值,然后更新。
6.2.2接收系统消息刷新:
比如intent-filter中再增加一个action:"android.provider.Telephony.SMS_RECEIVED",在AndroidMenifest.xml中任意位置添加<uses-permission android:name="android.permission.RECEIVE_SMS" />设置好权限,当系统有短消息收到时就能触发onReceive了。
但是有些Action比较特殊,比如android.intent.action.TIME_TICK,根据android.content.intent文档中的描述:You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().
这个Action在AndroidManifest.xml中声明了也没用,必须要自己开个服务注册receiver才能收到,然后再转发一次给自己。
6.3直接在外部Activity或者Service中刷新:
btnRefresh.setOnClickListener(new OnClickListener() { public void onClick(View v) { String value = teInput.getText().toString(); RemoteViews appWidgetView = new RemoteViews(SendMsgActivity.this.getPackageName(), R.layout.widget_layout_demo); appWidgetView.setTextViewText(R.id.tv1, value); appWidgetView.setTextViewText(R.id.tv2, value); AppWidgetManager.getInstance(SendMsgActivity.this) .updateAppWidget(new ComponentName(SendMsgActivity.this, AppWidgetDemo.class), appWidgetView); } });
此段代码可直接刷新AppWidget的内容,不会触发其onUpdate()。
7.响应点击事件
因为onUpdate是每个AppWidget被放置到桌面上时都会被呼叫的函数,所以在此函数中完成事件的关联:
Intent intent = new Intent(context, SendMsgActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); appWidgetView.setOnClickPendingIntent(R.id.tv1, pendingIntent);
另外要注意此段代码必须要在appWidgetManager.updateAppWidget()之前,否则是不会生效的。
运行后可以点击AppWidget的第一个控件,就能呼叫指定的Activity了。
8.Config Activity
这个可直接参考SDK文档中的Dev Guide-->App Widgets了。
评论
2 楼
等一个人咖啡
2012-11-10
onUpdate在AppWidget放到桌面时会被调用,在Update period millis达到时可能会被调用。
Update period millis为什么只是可能被调用呢
Update period millis为什么只是可能被调用呢
1 楼
shujun62
2011-02-25
如果文章中有demo运行效果插图,文章可读性会更好。
发表评论
-
使用googleapi-client-java操作gtasks(二)
2012-03-29 15:34 4584对于很多第三方的机器没有安装Google账户管理,要访问GTa ... -
Android Activity 生命周期再验证
2011-11-22 16:20 7990Android Activity 生命活动 ... -
Nexus one开关键坏掉后的解决方法
2011-10-14 15:17 4464Nexus one手机无法开机了,网上搜索了一下发现很多人遇到 ... -
改包名导致JNI调用失败的问题
2011-08-31 13:41 5144修改以前的代码,移动了一个文件,修改了其包名,在这个文件代码中 ... -
使用googleapi-client-java操作gtasks(一)
2011-08-10 18:07 14805Google Tasks的API终于开放 ... -
一个好用的Google api调试网址
2011-08-09 11:00 1816https://code.google.com/apis/ex ... -
Apk文件Hack试验
2011-07-11 17:54 2856试验1:反编译dex文 ... -
一个由onKeyUp引起的问题
2011-04-19 16:05 1921刚写一个小程序,在用户按下Back按键返回时,需要保存一些数据 ... -
Android自定义组合控件
2011-03-20 18:56 12821目标:实现textview和ImageButton组合,可以通 ... -
也谈Android下一个apk安装多个程序入口图标
2011-03-18 11:28 9190Android中有的Ap功能比较复杂,为了方便用户使用,可以提 ... -
Android Activity LaunchMode 验证
2011-02-23 15:04 8261在Android中,每个Activity有4种LaunchMo ... -
文件夹权限引起的MediaPlayer播放不正常
2011-01-20 20:41 2298写了一个Ap,在程序运行时会解压一些声音文件到/data/da ... -
关于使用InputStreamReader读取GBK编码文件乱码的问题
2011-01-19 17:13 25747BufferedReader reader = new Buf ... -
Android下Listview的onItemClick以及onItemLongClick等易模糊问题验证
2010-12-23 21:10 22758最近在使用Listview又遇到了以前碰到的问题,当Listv ... -
通过Java annotation以及反射机制实现不同类型通用的数据库访问接口
2010-12-22 12:14 5487在日常开发中会遇到这 ... -
Android PreferenceActivity 学习笔记
2010-12-11 12:45 20019在Ap中有时需要设置一些配置参数,这些参数通过配置文件保存。 ... -
Android平台sqlite快速入门
2010-12-07 12:00 2335以下概念都是在Android平台的sqlite3限制下的理解: ... -
Android下使用googleapi-client-java操作google calendar(三)
2010-11-29 18:57 47694.获取日历列表并显示: 原理:向https://www. ... -
Android下使用googleapi-client-java操作google calendar(二)
2010-11-28 22:50 23533.与服务器进行数据交互: 交互是通过HTTP请求及响应来进 ... -
Android下使用googleapi-client-java操作google calendar(一)
2010-11-19 14:41 4970刷机又把数据刷没了,网络时代,数据还是和服务器同步好了。 从 ...
相关推荐
7.5 Android应用的灵魂——Intent和Activity介绍与实例 7.5.1 实例操作演示 7.5.2 实例编程实现 7.6 用好列表,做好程序——列表(ListView)介绍与实例 7.6.1 实例程序演示 7.6.2 实例编程实现 7.7 友好地互动交流...
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android....
4.30 实时配置桌面上的AppWidget UI Layout 4.31 识别输入装置ID与InputDevice装置 4.32 选取文字的聪明文字联想 4.33 设计与Launcher相同的三页滑动Layout 第5章 交互式通信服务与手机控制 5.1 具有正则表达式的...
4.30 实时配置桌面上的AppWidget UI Layout 4.31 识别输入装置ID与InputDevice装置 4.32 选取文字的聪明文字联想 4.33 设计与Launcher相同的三页滑动Layout 第5章 交互式通信服务与手机控制 5.1 具有正则表达式的...
4.30 实时配置桌面上的AppWidget UI Layout 4.31 识别输入装置ID与InputDevice装置 4.32 选取文字的聪明文字联想 4.33 设计与Launcher相同的三页滑动Layout 第5章 交互式通信服务与手机控制 5.1 具有正则表达式的...
4.30 实时配置桌面上的AppWidget UI Layout 4.31 识别输入装置ID与InputDevice装置 4.32 选取文字的聪明文字联想 4.33 设计与Launcher相同的三页滑动Layout 第5章 交互式通信服务与手机控制 5.1 具有正则表达式的...