Android App Widget的简单使用
App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分;
例如:



实现步骤及代码如下:
(1)首先,在AndroidManifest.xml中声明一个App Widget;
(1)定义AppWidgetProviderInfo对象:为App Widget提供元数据,包括布局、更新频率等,这个对象定义在XML文件当中;
在res/xml文件夹中定义一个名为example_appwidget_info.xml的文件;
(2)为App Widget指定样式和布局:
定义一个新的布局文件example_appwidget.xml;
(3)实现AppWidgetProvider:定义了App Widget的基本生命周期函数;
onUpdate:在到达指定的更新时间之后或当前用户向桌面添加App Widget时调用该方法;
onDeleted:当App Widget被删除时,会调用该方法;
onEnabled:当一个App Widget的实例第一次被创建时,会调用该方法;
onDisabled:当最后一个App Widget实例被删除后,会调用该方法;
onReveice:接收广播事件;
详细实现步骤可参考官方文档,讲解的十分详细:
http://android.toolib.net/guide/topics/appwidgets/index.html
http://developers.androidcn.com/guide/topics/appwidgets/index.html#Providers
代码:
AndroidManifest.xml中声明 App Widget:
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
res/xml目录下的example_appwidget_info.xml:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/example_appwidget"
android:minHeight="294dp"
android:minWidth="72dp"
android:updatePeriodMillis="86400000" >
</appwidget-provider>
新的布局文件 example_appwidget.xml:
<?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" >
<TextView
android:id="@+id/widgetTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFCC"
android:text="widget是一些桌面的小插件" />
</LinearLayout>
定义类ExampleAppWidgetProvider继承AppWidgetProvider:
package com.example.appwidgettest2;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println("onUpdate");
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
System.out.println("onDeleted");
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
System.out.println("onDisabled");
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
System.out.println("onEnabled");
super.onEnabled(context);
}
}

删除一个App Widget:

全部删除:

在此基础上,来看另一个例子:
给原先的App Widget添加一个按钮,点击后能跳转到另一个Activity;


实现方式为:
使用PendingIntent和RemoteViews来实现;
(1)创建PendingIntent的方法有3种;
getActivity(Context context, int requestCode, Intent intent, int flags) getBroadcast(Context context, int requestCode, Intent intent, int flags) getService(Context context, int requestCode, Intent intent, int flags)
(2)RemoteViews的作用:
RemoteViews对象表示了一系列的View对象;
RemoteViews所代表的对象运行在另外的线程当中;
(3)因为App Widget和我们的应用程序运行在不同的进程当中(App Widget当中的View运行在Home Screen进程当中),所以无法按照之前那种方式绑定监听器;
而是应该使用remoteViews.setOnClickPendingIntent(R.id.widgetButtonId,pendingIntent)来实现;
TargetActivity.java
package com.example.appwidgettest2;
import android.app.Activity;
import android.os.Bundle;
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity);
}
}
target_activity.xml
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:text="跳转到这个Activity" />
</LinearLayout>
而ExampleAppWidgetProvider修改为:
package com.example.appwidgettest2;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println("onUpdate");
for (int i = 0; i < appWidgetIds.length; i++) {
System.out.println(appWidgetIds[i]);
// 创建一个Intent对象
Intent intent = new Intent(context, TargetActivity.class);
// 创建一个PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
// 为按钮绑定事件处理器,
// 第一个参数用来指定被绑定处理器控件的ID;
// 第二个参数用来指定当事件发生时,哪个PendingIntent将会被指定;
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId,
pendingIntent);
// 更新AppWidget
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
}
再来看另一个例子:使用RemoteViews对象更新AppWidget当中控件的状态;
效果如下:

点击按钮,更新状态:

不过需要注意的是:
(1)要更新控件的状态,需要用到广播机制,要使用getBroadcast来创建PendingIntent;
(2)在使用RemoteViews更新控件状态之后,需要使用AppWidgetManager通知AppWidget进行更新;
代码:
AndroidManifest.xml添加receiver:
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaozhang.UPDATE_APP_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
example_appwidget.xml
<?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" >
<TextView
android:id="@+id/widgetTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCFFCC"
android:textSize="20sp"
android:text="right.png" />
<ImageView
android:id="@+id/imageId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/right" />
<Button
android:id="@+id/widgetButtonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
ExampleAppWidgetProvider.java
package com.example.appwidgettest2;
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.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
final static String UPDATE_ACTION = "com.xiaozhang.UPDATE_APP_WIDGET";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent intent = new Intent();
intent.setAction(UPDATE_ACTION);
// 创建一个PendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1,
intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
// 为按钮绑定事件处理器,
// 第一个参数用来指定被绑定处理器控件的ID;
// 第二个参数用来指定当事件发生时,哪个PendingIntent将会被指定;
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
// 更新AppWidget
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UPDATE_ACTION.equals(action)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
remoteViews.setImageViewResource(R.id.imageId, R.drawable.wrong);
remoteViews.setTextViewText(R.id.widgetTextId, "wrong.png");
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
ComponentName componentName = new ComponentName(context,
ExampleAppWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
} else {
super.onReceive(context, intent);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
}
Android App Widget的简单使用的更多相关文章
- android app widget 创建调用周期
1 ?Android widget 大小问题 2 ?RemoteViewService Android开发历程_15(AppWidget的使用) Appwidget就是手机应用中常常放在桌面(即hom ...
- Android app widget中实现跑马灯效果(非widget部件也实用)
原文地址:http://blog.csdn.net/luoyebuguigen/article/details/37533631 关键是需要在TextView中嵌入requestForcus标签才会有 ...
- [安卓开发]App Widget开发入门指导
本节所要讲的主要内容包括Android桌面小部件.App Widget的开发入门指导,并通过一个简单实例的形式来直观的讲解App Widget. 一.Widget .App Widget .Web A ...
- Android APP 简单高效的禁用横竖屏切换
默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏 ...
- Android 之窗口小部件高级篇--App Widget 之 RemoteViews - 跨到对岸去
在之前的一篇博文( Android 之窗口小部件详解--App Widge t)中,已经介绍了App Widget的基本用法和简单实例.这篇主要讲解 App Widget 的高级内容,即通过 Remo ...
- Android 之窗口小部件高级篇--App Widget 之 RemoteViews
Android 之窗口小部件高级篇--App Widget 之 RemoteViews 在之前的一篇博文(Android 之窗口小部件详解--App Widget)中,已经介绍了App Widget的 ...
- App Widget简单应用
首先后台进程创建一个PendingIntent对象,其中PendingIntent中包含一个真正的Intent,创建完成后将此PendingIntent对象交给桌面控件所在的进程,当用户点击桌面控件或 ...
- Android 之窗口小部件详解--App Widget
Android 之窗口小部件详解--App Widget 版本号 说明 作者 日期 1.0 添加App Widge介绍和示例 Sky Wang 2013/06/27 1 App ...
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
随机推荐
- Apache Ant在Windows下配置环境变量
Windows下ANT用到的环境变量主要有2个: ANT_HOME 和 PATH. 1..设置ANT_HOME指向ant的安装目录,如下: ANT_HOME = D:\soft\apache\apac ...
- Java中setCharAt()方法介绍
--转载自网络,备忘 这是StringBuffer类里面的一个方法:主要是用来替换的,方法里面有两个参数setCharAt(int index,Char ch),第一个参数是取代的位置 索引从0开始 ...
- 常用的MIME类型(资源的媒体类型)
后缀名 MIME名称 *.3gpp audio/3gpp, video/3gpp *.ac3 audio/ac3 *.asf allpication/vnd.ms-asf *.au audio/bas ...
- 菜单栏始终浮动在顶部 js
//菜单栏始终浮动在顶部var navH = $(".trade-tab-bot").offset().top;//获取要定位元素距离浏览器顶部的距离//滚动条事件$(window ...
- mongodb授权登录
mongodb版本为3.2(目前最新),演示的是linux下的mongodb授权认证 第一次登录不启动授权(mongo默认不启动) ./mongod --dbpath=/home/db/data -- ...
- Java 实现Md5算法
package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * ...
- 【小程序开发】微信小程序开发中遇到的那些坑...
第一坑: 设置了三个tabBar,却默认显示第二个,不能展示我的第一个[首页]. "list": [{ "pagePath":"page/KTGJ/i ...
- 基于live555的视频直播 DM368IPNC RTSP分析
因需要,从个人的理解顺序和需求角度对live555的分析与开发整理,包含RTSP Server与RTSP Client.如何直播H.264流与JPEG流等,均进行了探讨,对live555的初学者有一定 ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- android repo库的创建及代码管理