appWidget开发实例
2014-02-16 14:36
267 查看
AppWidget框架类
widget 就是桌面小部件,首先我们来简单的介绍一下AppWidget的框架类。主要包括以下四个。AppWidgetProvider-----------基于BrodCast事件操作的AppWidget的接口,当appwidget应用update,enable,disable,delete时候,通过他们可以接收到BrodCast事件。其中onUpdate、OnReceive是常用的方法,用于接收更新通知。
AppWidgetProvderInfo---------用于描述Appwidget的元数据对象,如大小,更新频率、初始化界面和AppWidgetProvider类等信息。以xml的形式存在于res/xml的文件夹下面。
AppWidgetManager---------管理appwidget,像appwidgetprovider发送通知。
RemoteViews--------可以在其他应用进程中运行的类,像AppWidgetProvider发送通知。
开发实例
首先新建一个工程,然后在layout文件夹下写好widget的布局文件这里我们命名为appwidgetlayout.xml,就是显示在桌面上的布局。不过要注意的是并不是所有的控件widget都支持。目前支持的有FrameLayout、LinearLayout、RelativeLayout、AnalogClock、Button 、Chronometer 、ImageButton、ImageView 、ProgessBar、TextView、ViewFlipper..首先给出的是布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" /> </LinearLayout>
然后就是配置我们的widget在res文件夹下面新建一个xml文件夹在里面新建一个配置文件我们命名为appwidget1.xml
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="200dp" android:minWidth="200dp" android:initialLayout="@layout/appwidgetlayout" android:updatePeriodMillis="888888" > </appwidget-provider>最后就是编写代码来控制了。这里我们继承AppWidgetProvider然后我们来看看代码的实现。
public class MyAppWidgetProvider extends AppWidgetProvider { private final String brodCastString="com.bobo.widgetupdate"; //删除一个widget时候调用 @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); } //最后一个widget删除的时候调用 @Override public void onDisabled(Context context) { // TODO Auto-generated method stub super.onDisabled(context); } //第一次创建的时候调用 @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); } /** * 接收广播事件 */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); if(intent.getAction().equals(brodCastString)) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName componentName = new ComponentName(context, MyAppWidgetProvider.class); remoteViews.setTextViewText(R.id.text, "点击"); appWidgetManager.updateAppWidget(componentName, remoteViews); } } //到达指定更新时间或者用户向桌面添加widget时候调用 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); Intent intent = new Intent(); intent.setAction(brodCastString); PendingIntent intent2 = PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout); remoteViews.setOnClickPendingIntent(R.id.send, intent2); remoteViews.setTextViewText(R.id.text, "hehe"); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); } }
最有需要在我们的androidmainifest文件里面注册我们的广播。
<receiver android:name="com.example.remoteviewdemo.MyWidgetProvider" > <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidgetprovider" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="com.bobo.widgetupdate" /> </intent-filter> </receiver>
相关文章推荐
- MyBatis实例教程--开发环境搭建
- 基于Eclipse的hadoop开发环境配置及worldCount实例运行
- ArcGIS Engine+C#实例开发教程
- iOS开发- UICollectionView详解+实例
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(二)
- Windows Phone实例开发:快递查询助手 - [WP开发]
- MMC/SD卡驱动实例开发讲解(一)
- ios开发-UI-UITableView及一个小实例
- ThinkPHP的MVC开发机制实例解析
- 第五篇T语言实例开发,数组空间使用(版本5.3)
- Android+PHP+MySQL开发简单实例
- 第二篇T语言实例开发(版本5.3),福彩3D摇号器
- Axis2和Tomcat6.0开发Web Service环境配置 测试和实例 整理
- 【原创】《Linux高级程序设计》杨宗德著 - 进程管理与程序开发 - 信号应用实例
- Struts+Spring+Hibernate开发实例
- C语言模块化开发简单实例——实现一个计算器的加减乘除
- AgileEAS.NET平台开发实例-药店系统-数据库建模
- Spring的开发配置与Spring中bean的实例化
- 《Cocos2d-x for iPhone游戏开发实例详解---1.2 绘制精灵》
- java开发之spring webflow实现上传单个文件及多个文件功能实例