setRemoteAdapter (int viewId, Intent intent):该方法可以使用 Intent 更新 RemoteViews 中viewId 对应的组件。

上面方法的 Intent 参数应该封装一个 RemoteViewsService 参数,RemoteViewsService 虽然继承了 Service 组件,但它的主要作用是为 RemoteViews 中 viewId 对应的组件提供列表项。

由于Intent参数负责提供列表项,因此viewId参数对应的组件可以是ListView、GridView、StackView 和 AdapterViewFlipper 等,这些组件都是 AdapterView 的子类,由此可见RemoteViewsService 负责提供的对象,应该是一个类似于 Adapter 的对象。

RemoteViewsService 通常用于被继承,继承该基类时需要重写它的 onGetViewFactory()方 法 , 该 方 法 就 需 要 返 回 一 个 类 似 于 Adapterr 对 象 —— 但 不 是 Adapter , 而 是RemoteViewsFactory 对象,RemoteViewsFactory 的功能完全类似于 Adapter。

StackViewWidget实现如下:

StackWidgetService.java

package org.crazyit.desktop;

import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService; /**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class StackWidgetService extends RemoteViewsService
{
// 重写该方法,该方法返回一个RemoteViewsFactory对象。
// RemoteViewsFactory对象的的作用类似于Adapter,
// 它负责为RemoteView中指定组件提供多个列表项。
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent)
{
return new StackRemoteViewsFactory(this.getApplicationContext(),
intent); //①
}
class StackRemoteViewsFactory implements
RemoteViewsService.RemoteViewsFactory
{
// 定义一个数组来保存该组件生成的多个列表项
private int[] items = null;
private Context mContext;
public StackRemoteViewsFactory(Context context, Intent intent)
{
mContext = context;
}
@Override
public void onCreate()
{
// 初始化items数组
items = new int[] { R.drawable.bomb5, R.drawable.bomb6,
R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9,
R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12,
R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15,
R.drawable.bomb16
};
}
@Override
public void onDestroy()
{
items = null;
}
// 该方法的返回值控制该对象包含多少个列表项
@Override
public int getCount()
{
return items.length;
}
// 该方法的返回值控制各位置所显示的RemoteViews
@Override
public RemoteViews getViewAt(int position)
{
// 创建RemoteViews对象,加载/res/layout目录下widget_item.xml文件
RemoteViews rv = new RemoteViews(mContext.getPackageName(),
R.layout.widget_item);
// 更新widget_item.xml布局文件中的widget_item组件
rv.setImageViewResource(R.id.widget_item,
items[position]);
// 创建Intent、用于传递数据
Intent fillInIntent = new Intent();
fillInIntent.putExtra(StackWidgetProvider.EXTRA_ITEM, position);
// 设置当单击该RemoteViews时传递fillInIntent包含的数据
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
// 此处使用让线程暂停0.5秒来模拟加载该组件
try
{
System.out.println("加载【" + position + "】位置的组件");
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return rv;
}
@Override
public RemoteViews getLoadingView()
{
return null;
}
@Override
public int getViewTypeCount()
{
return 1;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public void onDataSetChanged()
{
}
}
}

widget_item.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="120dp"
android:layout_height="120dp"
android:gravity="center"/>

StackWidgetProvider.java

package org.crazyit.desktop;

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;
import android.widget.Toast; /**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class StackWidgetProvider extends AppWidgetProvider
{
public static final String TOAST_ACTION
= "org.crazyit.desktop.TOAST_ACTION";
public static final String EXTRA_ITEM
= "org.crazyit.desktop.EXTRA_ITEM"; @Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// 创建RemoteViews对象,加载/res/layout目录下的widget_layout.xml文件
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent intent = new Intent(context, StackWidgetService.class);
// 使用intent更新rv中stack_view组件(StackView)
rv.setRemoteAdapter(R.id.stack_view, intent); //①
// 设置当StackWidgetService提供的列表项为空时,直接显示empty_view组件
rv.setEmptyView(R.id.stack_view, R.id.empty_view);
// 创建启动StackWidgetProvider组件(作为BroadcastReceiver)的Intent
Intent toastIntent = new Intent(context,
StackWidgetProvider.class);
// 为该Intent设置Action属性
toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
// 将Intent包装成PendingIntent
PendingIntent toastPendingIntent = PendingIntent
.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 将PendingIntent与stack_view进行关联
rv.setPendingIntentTemplate(R.id.stack_view,
toastPendingIntent);
// 使用AppWidgetManager通过RemteViews更新AppWidgetProvider
appWidgetManager.updateAppWidget(
new ComponentName(context, StackWidgetProvider.class), rv); //②
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);
}
// 重写该方法,将该组件当成BroadcastReceiver使用
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(TOAST_ACTION))
{
// 获取Intent中的数据
int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
// 显示Toast提示
Toast.makeText(context, "点击第【" + viewIndex + "】个列表项",
Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
}

widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<StackView
android:id="@+id/stack_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:loopViews="true" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#ff0f"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="@string/no_item"
android:textSize="20sp" />
</FrameLayout>

Manifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.crazyit.desktop"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:label="@string/app_name">
<!-- 配置AppWidgetProvider,即配置桌面控件 -->
<receiver android:name=".StackWidgetProvider">
<!-- 通过该intent-filter指定该Receiver作为桌面控件 -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<!-- 为桌面控件指定meta-data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/stackwidgetinfo" />
</receiver>
<!-- 配置RemoteViewsService
必须指定权限为android.permission.BIND_REMOTEVIEWS
-->
<service
android:name=".StackWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"
android:exported="false" />
</application>
</manifest>

stackwidgetinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="110dp"
android:minHeight="110dp"
android:updatePeriodMillis="3600000"
android:previewImage="@drawable/ic_launcher"
android:initialLayout="@layout/widget_layout"
android:resizeMode="horizontal|vertical"
android:autoAdvanceViewId="@id/stack_view">
</appwidget-provider>

截图:

Android 4.0 新增的显示数据集的桌面控件的更多相关文章

  1. Android 4.0新增Space及GridLayout初谈

    Android 4.0的SDK已经发布,在众多的新增特性中,其中对开发者来说比较重要的特性之一,是新增的两种界面布局方式:Space和Gridlayout,它们跟以往Android版本的sdk有什么不 ...

  2. Android开发中几种有用的的日历控件实现

    我们大家都知道,在Android平台3.0中才新增了日历视图控件,可以显示网格状的日历内容,那么对于3.0以下的版本要使用日历控件只能借助第三方,目前用的最多的是CalendarView. 先简单介绍 ...

  3. Pro Android 4 第六章 构建用户界面以及使用控件(一)

         目前为止,我们已经介绍了android的基础内容,但是还没开始接触用户界面(UI).本章我们将开始探讨用户界面和控件.我们先讨论一下android中UI设计的一般原理,然后我们在介绍一下an ...

  4. C# 时间控件 竖直进度条 饼图显示 仪表盘 按钮基础控件库

    Prepare 本文将使用一个NuGet公开的组件来实现一些特殊的控件显示,方便大家进行快速的开发系统. 在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台 ...

  5. WebBrowser无法显示招商银行password输入控件的问题

    本文由CharlesSimonyi发表于CSDN博客:http://blog.csdn.net/charlessimonyi/article/details/30479131转载请注明出处 之前就看到 ...

  6. SilverLight:基础控件使用(4)-日期显示和选择类控件

    ylbtech-SilverLight-Basic-Control:基础控件使用(4)-日期显示和选择类控件 Calendar,DatePicker 1.A,返回顶部 Calendar控件(日期控件) ...

  7. Android 7.0 新增功能和api

    Android 7.0 Nougat 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 请务必查阅 Android 7.0 行为变更以了解平台变更可能影响您的应用的领域. 要详细了解 A ...

  8. Android 8.0+ 通知不显示的适配

    最近在 写项目的时候  发现 通知并不会显示的问题,查看资料发现 从Android 8.0开始通知必须加上ChannelId Android O 引入了 通知渠道(Notification Chann ...

  9. Android 7.0 Dialog 无法显示的问题

    app 在 Android 7.0 上登录的时候, Dialog 不显示了,但是半透明背景显示 经过搜索和对比,发现出现该问题是因为重写了 getResources() 方法造成的 .重写该方法是为了 ...

随机推荐

  1. readmine项目管理和缺陷跟踪工具

    官方网站:http://www.redmine.org/演示地址:http://demo.redmine.org/下载地址:http://www.redmine.org/projects/redmin ...

  2. Android的minSdkVersion,targetSdkVersion,maxSdkVersion

    参考http://developer.android.com/guide/topics/manifest/uses-sdk-element.html API Level 是一个整型值,表示Androi ...

  3. Android中View和ViewGroup介绍

    1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...

  4. Android 开源项目android-open-project解析之(三) ScrollView,TimeView,TipView,FlipView

    九.ScrollView Discrollview 支持滚动时Item淡入淡出,平移,缩放效果的ScrollView 项目地址:https://github.com/flavienlaurent/di ...

  5. sync fsync fdatasync ---systemtap跟踪

    aa.stp: probe kernel .function ( "sys_sync" ) { printf ( "probfunc:%s fun:%s\n", ...

  6. Android(java)学习笔记246:ContentProvider使用之学习ContentProvider(内容提供者)的目的

    1.使用ContentProvider,把应用程序私有的数据暴露给别的应用程序,让别的应用程序完成对自己私有的数据库数据的增删改查的操作. 2.ContentProvider的应用场景: 获取手机系统 ...

  7. Hibernate HQL查询:

    Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查 ...

  8. jrae源码解析(一)

    jare用java实现了论文<Semi-Supervised Recursive Autoencoders for Predicting Sentiment Distributions>中 ...

  9. Notepad++中查找替换回车符

    用Nodepad++打开文件 View->Show Symbol->Show End of Line "End of Line"行结束符,由"CR" ...

  10. ThinkPHP 的CURD 基本操作

    说起CURD,懂点SQL的人都知道,就是增删改查,做业务系统的时候,往往离不开这CURD,最近也是刚刚接触ThinkPHP,ThinkPHP的灵活性是比原生PHP好用的多,下面我就简单的介绍一下我的学 ...