Android 4.0 新增的显示数据集的桌面控件

时间:2022-04-27 18:41:39

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 新增的显示数据集的桌面控件

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

  1. Android 4&period;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&num; 时间控件 竖直进度条 饼图显示 仪表盘 按钮基础控件库

    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&period;0 新增功能和api

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

  8. Android 8&period;0&plus; 通知不显示的适配

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

  9. Android 7&period;0 Dialog 无法显示的问题

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

随机推荐

  1. java Thread编程&lpar;三&rpar; 同步的两种不同实现方式

    1,创建需要同步的对象(方式一) package concurrency; public class Bank { private double amount; public Bank(double ...

  2. 【整理】JavaEE基本框架(Struts2&plus;Spring&plus;MyBatis三层,Struts MVC)之间的关系

    #[整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 ![关系图解](http://images.cnitblog.com/blog/84 ...

  3. &lbrack;ES7&rsqb; Descorator&colon; evaluated &amp&semi; call order

    When multiple decorators apply to a single declaration, their evaluation is similar to function comp ...

  4. 3星&vert;《陈志武金融投资课》:金融改善社会,A股投资策略

    从历史上的金融说起,介绍金融的基本知识.理念.大事.重要人物.也有一些A股投资策略和A股政策点评. 引用了不少学术研究成果做证据.讲历史的部分,功力比专业历史学者稍逊,毕竟这不是作者的专业. 我读后认 ...

  5. dirty&lowbar;background&lowbar;ration 与 &sol;proc&sol;sys&sol;vm&sol;dirty&lowbar;ratio

    wappiness的值的大小对如何使用swap分区是有着很大的联系的.swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用s ...

  6. &lbrack;BUAA&lowbar;SE&lowbar;2017&rsqb;案例分析-Week3

    Week3 案例分析 一.调研评测 案例: 神策数据的数据概览功能 Demo: 电商类产品Demo 评价: d) 好,不错 个人评价:神策数据电商类产品Demo的数据概览功能是相当不错的.首先点击进入 ...

  7. angular4 组件通讯、生命周期

    主要通讯形式 父组件通过属性绑定到子组件,子组件通过事件传递参数到父组件 父组件通过局部变量获取子组件的引用 父组件使用@ViewChild获取子组件的引用 两个不相关联的组件使用中间人模式交互 终极 ...

  8. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  9. Google Chrome插件分享

    前言 浏览器是大家日常使用最多的工具之一,对于程序员来说,Google Chrome浏览器当然是大家优选的最爱之一.面对Chrome丰富的插件真的是爱不释手,如何把自己的Chrome调教成自己心仪的样 ...

  10. 解决应用程序无法正常启动0xc0150002等问题

    1.在程序运行出错的时候,右键“我的电脑”,然后点击“管理”→“事件查看器”→“Windows 日志”→“应用程序”,查看错误信息: 1> “E:\IPCam_share\ARP\數據處理\Hg ...