android之Widget开发详解实例二
2013-08-18 11:20
591 查看
下面是本篇的大纲:
1、AppWidget 框架类
2、AppWidget 框架的主要类介绍
3、DEMO 讲解
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、Demo讲解
下面是我今天做的一个实例,提供给大家练习时做参考,效果如下:在布局中放一个 TextView 做桌面组件,然后设置TextView 的 Clickable="true" 使其有点击的功能,然后我们点击它时改变它的字体,再点击时变回来,详细操作如下流程:
1、新建AppWidgetProvderInfo
2、写一个类继承自AppWidgetProvider
3、后台注册Receiver
4、使 AppWidget 组件支持点击事件
5、如何使TextView 在两种文本间来回跳转
问题抛出来了,那么一起解决它吧。
1、新建AppWidgetProvderInfo
代码如下:
Tip:上文说过AppWidgetProvderInfo 是在res/xml 的文件形式存在的,看参数不难理解,就是定义widget的大小和更新频率等信息,比较重要的是这里android:initialLayout="@layout/main" 此句为指定桌面组件的布局文件,即规划widget里面有哪些组件,如何排列等。
2、写一个类继承自AppWidgetProvider
代码如下:
重写两个方法:1.onUpdate 为组件在桌面上生成时调用,并更新组件UI,
2.onReceiver 为接收广播时调用更新UI,一般这两个方法是比较常用的。
3、后台注册Receiver
后台配置文件代码如下:
Tip:因为是桌面组件,所以暂时不考虑使用Activity 界面,当然你在实现做项目时可能会需要点击时跳转到Activity 应用程序上做操作,典型的案例为Android 提供的音乐播放器。
上面代码中比较重要的是这一句 <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider"></meta-data>
大意为指定桌面应用程序的AppWidgetProvderInfo 文件,使其可作其管理文件,管理widget的大小更新时间等信息.
4、使
AppWidget 组件支持点击事件
先看代码:
此方法为创建组件时 onUpdate 调用的更新UI的方法,代码中使用RemoteView 找到组件的布局文件,同时为其设置广播***CLICK_NAME_ACTION并且通过RemoteView 的setOnClickPendingIntent 方法找到我想触发事件的TextView 为其设置广播。接着
在onReceiver 中通过判断识别传进来的广播来触发动作。
5、如何使TextView 在两种文本间来回跳转
如果你发现无法实现文本的跳转变化,尝试把public static boolean isChange=true;这个boolean放在新建一个类里面试试看.
还有main.xml的代码如下:
1、AppWidget 框架类
2、AppWidget 框架的主要类介绍
3、DEMO 讲解
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、Demo讲解
下面是我今天做的一个实例,提供给大家练习时做参考,效果如下:在布局中放一个 TextView 做桌面组件,然后设置TextView 的 Clickable="true" 使其有点击的功能,然后我们点击它时改变它的字体,再点击时变回来,详细操作如下流程:
1、新建AppWidgetProvderInfo
2、写一个类继承自AppWidgetProvider
3、后台注册Receiver
4、使 AppWidget 组件支持点击事件
5、如何使TextView 在两种文本间来回跳转
问题抛出来了,那么一起解决它吧。
1、新建AppWidgetProvderInfo
代码如下:
<?xml version="1.0" encoding="UTF-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="60dp" android:minHeight="30dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/main"> </appwidget-provider>
Tip:上文说过AppWidgetProvderInfo 是在res/xml 的文件形式存在的,看参数不难理解,就是定义widget的大小和更新频率等信息,比较重要的是这里android:initialLayout="@layout/main" 此句为指定桌面组件的布局文件,即规划widget里面有哪些组件,如何排列等。
2、写一个类继承自AppWidgetProvider
代码如下:
package com.terry; public class widgetProvider extends AppWidgetProvider { private static final String CLICK_NAME_ACTION = "com.terry.action.widget.click"; public static boolean isChange=true; private static RemoteViews rv; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); if (rv == null) { rv = new RemoteViews(context.getPackageName(), R.layout.main); } if (intent.getAction().equals(CLICK_NAME_ACTION)) { if (isChange) { rv.setTextViewText(R.id.TextView01, context.getResources() .getString(R.string.load)); } else { rv.setTextViewText(R.id.TextView01, context.getResources() .getString(R.string.change)); } Toast.makeText(context, Boolean.toString(isChange), Toast.LENGTH_LONG).show(); isChange = !isChange; } AppWidgetManager appWidgetManger = AppWidgetManager .getInstance(context); int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName( context, widgetProvider.class)); appWidgetManger.updateAppWidget(appIds, rv); } public static void updateAppWidget(Context context, AppWidgetManager appWidgeManger, int appWidgetId) { rv = new RemoteViews(context.getPackageName(), R.layout.main); Intent intentClick = new Intent(CLICK_NAME_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0); rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent); appWidgeManger.updateAppWidget(appWidgetId, rv); } }
重写两个方法:1.onUpdate 为组件在桌面上生成时调用,并更新组件UI,
2.onReceiver 为接收广播时调用更新UI,一般这两个方法是比较常用的。
3、后台注册Receiver
后台配置文件代码如下:
<receiver android:name=".widgetProvider"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider"></meta-data> <intent-filter> <action android:name="com.terry.action.widget.click"></action> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver>
Tip:因为是桌面组件,所以暂时不考虑使用Activity 界面,当然你在实现做项目时可能会需要点击时跳转到Activity 应用程序上做操作,典型的案例为Android 提供的音乐播放器。
上面代码中比较重要的是这一句 <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider"></meta-data>
大意为指定桌面应用程序的AppWidgetProvderInfo 文件,使其可作其管理文件,管理widget的大小更新时间等信息.
4、使
AppWidget 组件支持点击事件
先看代码:
public static void updateAppWidget(Context context, AppWidgetManager appWidgeManger, int appWidgetId) { rv = new RemoteViews(context.getPackageName(), R.layout.main); Intent intentClick = new Intent(CLICK_NAME_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0); rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent); appWidgeManger.updateAppWidget(appWidgetId, rv); }
此方法为创建组件时 onUpdate 调用的更新UI的方法,代码中使用RemoteView 找到组件的布局文件,同时为其设置广播***CLICK_NAME_ACTION并且通过RemoteView 的setOnClickPendingIntent 方法找到我想触发事件的TextView 为其设置广播。接着
@Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); if (rv == null) { rv = new RemoteViews(context.getPackageName(), R.layout.main); } if (intent.getAction().equals(CLICK_NAME_ACTION)) { if (isChange) { rv.setTextViewText(R.id.TextView01, context.getResources() .getString(R.string.load)); } else { rv.setTextViewText(R.id.TextView01, context.getResources() .getString(R.string.change)); } Toast.makeText(context, Boolean.toString(isChange), Toast.LENGTH_LONG).show(); isChange = !isChange; } AppWidgetManager appWidgetManger = AppWidgetManager .getInstance(context); int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName( context, widgetProvider.class)); appWidgetManger.updateAppWidget(appIds, rv); }
在onReceiver 中通过判断识别传进来的广播来触发动作。
5、如何使TextView 在两种文本间来回跳转
如果你发现无法实现文本的跳转变化,尝试把public static boolean isChange=true;这个boolean放在新建一个类里面试试看.
还有main.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:id="@+id/layout" android:layout_height="fill_parent"> <TextView android:text="@string/load" android:id="@+id/TextView01" android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>
相关文章推荐
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (上)
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (上)
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (上)
- android之Widget开发详解实例二
- android之Widget开发详解实例一
- android之Widget开发详解实例一
- android之Widget开发详解实例三
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (下)
- Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)
- Android开发之TabActivity用法实例详解
- android界面开发:ViewPager的详解,并用于仿微博滑动实例和图片滚动实例
- Android 音乐播放器的开发实例详解
- Android 开发订单流程view实例详解
- Android 开发照相功能实例详解
- Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)
- Android 记事本开发实例详解
- Android开发之Android.mk模板的实例详解
- ndk开发中的Android.mk文件与Application.mk详解及实例
- 【Android开发】BitMap优化最佳实践及官方Demo“图片墙”实例详解
- Android开发之电话拨号器实例详解