Android 实时文件夹
实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件。要创建一个实时文件夹,必须要有两个方面的支持。
1,要定义一个用来创建实时文件夹的Activity。
2,所指定数据信息URI的ContentProvider必须支持实时文件夹时文件夹查询
一、定义创建实时文件夹的Activity
想在桌面长按后选择实时文件夹就会弹出一个可用实时文件夹的列表对话框,必须在应用程序内的Activity中添加一个Action为android.intent.action.CREATE_LIVE_FOLDER的IntentFilter。而在这个创建实时文件夹的Activity中,我们要把实时文件夹的信息以附加信息的形式存储在一个Intent对象当中。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex2"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon"
- android:label="@string/app_name">
- <activity android:name=".MyAllContacts"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name=
- "android.intent.action.CREATE_LIVE_FOLDER" />
- <category android:name=
- "android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.studio.android.ch10.ex2"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".MyAllContacts"
android:label="@string/app_name">
<intent-filter>
<action android:name=
"android.intent.action.CREATE_LIVE_FOLDER" />
<category android:name=
"android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>

由于Content的ContentProvider已经实现了对实时文件夹的相关支持
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.Contacts;
- import android.provider.LiveFolders;
- public class MyAllContacts extends Activity {
- public static final Uri LIVE_FOLDER_URI =
- Uri.parse("content://contacts/live_folders/people");
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getIntent().getAction()
- .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
- Intent intent = new Intent();
- intent.setData(LIVE_FOLDER_URI);//在文件夹,对于要查询的URI则是以Data的形式存储在Intent对象中。Contacts的ContentProvider已经实现了对实时文件夹的相关支持。
- intent.putExtra(
- LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT,
- new Intent(Intent.ACTION_VIEW,
- Contacts.People.CONTENT_URI));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
- "MyAllContacts");
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.icon));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
- LiveFolders.DISPLAY_MODE_LIST);//还可以设置LiveFolders.DISPLAY_MODE_LIST
- setResult(RESULT_OK, intent);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.LiveFolders; public class MyAllContacts extends Activity {
public static final Uri LIVE_FOLDER_URI =
Uri.parse("content://contacts/live_folders/people"); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); if (getIntent().getAction()
.equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) { Intent intent = new Intent(); intent.setData(LIVE_FOLDER_URI);//在文件夹,对于要查询的URI则是以Data的形式存储在Intent对象中。Contacts的ContentProvider已经实现了对实时文件夹的相关支持。
intent.putExtra(
LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT,
new Intent(Intent.ACTION_VIEW,
Contacts.People.CONTENT_URI));
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
"MyAllContacts");
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.icon));
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
LiveFolders.DISPLAY_MODE_LIST);//还可以设置LiveFolders.DISPLAY_MODE_LIST setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
} finish();
}
}
二、定义支持实时文件夹的ContentProvider
要使一个ContentProvider支持实时文件夹的查询,主要要实现下面2个:
1,为实时文件夹查询定义一个专门的URI
2,在query查询方法中针对实时文件夹的路径进行相应的查询然后返回含有特定列名的Cursor

在CountryCode.java中
//为URI匹配器增加实时文件夹URI的匹配号码
public static final int LIVE_FOLDER = 3;
---
---
---
//定义实时文件夹的URI
public static final Uri LIVE_FOLDER_URI =
Uri.parse("content://" + AUTHORITY + "/livefolder");
在MyProvider.java中
static {
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
----
---
sMatcher.addURI(CountryCode.AUTHORITY,
"livefolder/", CountryCode.LIVE_FOLDER);
}
---
---
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] args,String order) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c;
switch (sMatcher.match(uri)) {
----
case CountryCode.LIVE_FOLDER:
String[] myProjection = {
//注意更改别名
CountryCode.ID + " AS " + LiveFolders._ID,
CountryCode.COUNTRY + " AS " + LiveFolders.NAME,
CountryCode.CODE + " AS " + LiveFolders.DESCRIPTION
};
c = db.query(CountryCode.TB_NAME, myProjection, selection,
args,null,null,order);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
CreateLiveFolder.java中
import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.LiveFolders;
- public class CreateLiveFolder extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getIntent().getAction()
- .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
- Intent intent = new Intent();
- intent.setData(CountryCode.LIVE_FOLDER_URI);
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
- "CountryCode");
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.icon));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
- LiveFolders.DISPLAY_MODE_LIST);
- setResult(RESULT_OK, intent);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
import android.content.Intent;
import android.os.Bundle;
import android.provider.LiveFolders; public class CreateLiveFolder extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); if (getIntent().getAction()
.equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) { Intent intent = new Intent(); intent.setData(CountryCode.LIVE_FOLDER_URI);
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
"CountryCode");
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.icon));
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
LiveFolders.DISPLAY_MODE_LIST); setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
}
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.chp10.ex3"
- android:versionCode="1"
- android:versionName="1.0.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SQLite2"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <provider android:name="MyProvider"
- android:authorities="com.studio.andriod.provider.countrycode" />
- <activity android:name=".CreateLiveFolder">
- <intent-filter>
- <action android:name=
- "android.intent.action.CREATE_LIVE_FOLDER" />
- <category android:name=
- "android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.studio.android.chp10.ex3"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SQLite2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="MyProvider"
android:authorities="com.studio.andriod.provider.countrycode" /> <activity android:name=".CreateLiveFolder">
<intent-filter>
<action android:name=
"android.intent.action.CREATE_LIVE_FOLDER" />
<category android:name=
"android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
- UrgentCall.rar (35.8 KB)
- MyAllContacts.rar (23.4 KB)
Android 实时文件夹的更多相关文章
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 public class FileSizeUtil { public static final int SIZETYPE_B ...
- 使用实时文件夹显示ContentProvider的数据
所谓实时文件夹(即LiveFolder),是指用于显示ContentProvider提供的数据的桌面组件. ContentProvider用于向外提供数据访问的接口,一个应用程序可通过ContentP ...
- (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...
- android assets文件夹浅谈
---恢复内容开始--- 最近在研究assets文件夹的一些属性跟使用方法.根据网上一些文章.实例做一下汇总,拿出来跟大家分享下,有不足的地方还请多多指教. 首先了解一下assets是干什么用的,as ...
- Android 建立文件夹、生成文件并写入文本文件内容
一.首先添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">& ...
- Android布局文件夹引起的问题
Android 运行到setContentView(R.layout.splash); 总是出现如下的错误: java.lang.RuntimeException: Unable to start a ...
- 分析cocos2d-x在Android上的编译过程(1):cocco2d-x是怎样生成的Android的文件夹结构
当新建完一个cocos2d-x的项目后.进入到项目中的proj.android中,会看到例如以下的文件夹结构 在VS先把它编译,然后导入到Eclipse中,导入完后会看到多了几个文件 watermar ...
- Android assets文件夹之位置放置和作用
Android 的assets文件夹的放置位置,Eclipse创建项目时就生成了的,Android Studio则不太一样,AS可以包含几种方式, 1:可以在build.gradle文件下配置,加如下 ...
- android project 文件夹
android多国语言文件夹 http://www.blogjava.net/zhaojianhua/archive/2012/02/09/369676.html Android平板开发精确适配不同的 ...
随机推荐
- Window 10通过网线和Wifi连接树莓派
几个月前买了个树莓派,扔在一边没有捣鼓,今天搞定了笔记本通过家里的wifi登录树莓派,下面列出设置过程. 实验环境: 网络:只有wifi 材料:笔记本一台(Win10),树莓派一台,EDUP USB无 ...
- Java设计模式---工厂模式(简单工厂、工厂方法、抽象工厂)
工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类.工厂模式的形态工厂模式主要用一下几种形态:1:简单工厂(Simple Factory).2:工厂方法(Factory M ...
- Linux中/etc/passwd文件与/etc/shadow文件解析.
此文章转载自"慧可",用来学习. 1. /etc/passwd文件 1.1 /etc/passwd文件内容格式 用户名: 密码 : uid : gid :用户描述:主目录:登陆s ...
- Apache Tomcat 7.0 Manager APP
解决Tomcat 7.0 进入项目管理页面时的密码问题 根据红框中的提示得知缺少manager管理角色,所以我们到Tomcat的conf文件夹下找到tomcat-users.xml文件,添加管理角色, ...
- Asp.Net--上传大文件(页面超时)
几个方法: 修改文件上传大小的限制 以文件形式保存到服务器 转换成二进制字节流保存到数据库 将二进制通过循环的方式写入磁盘 一.修改文件上传大小的限制 通过对web.config和machine.co ...
- JS 匿名函数
一.声明: 1. 正常函数声明: //正常函数声明 function foo(p1, p2){ return p1+p2; } 2. 匿名函数声明: //匿名函数声明 var foo= functio ...
- 你好,C++(20).4.2.2 表达并列条件选择的switch语句:如果……如果……如果……
4.2.2 表达并列条件选择的switch语句:如果……如果……如果…… 在现实世界中,还有这样一类特殊的条件选择: 如果明天是晴天,我就穿T恤: 如果明天是阴天,我就穿衬衣: 如果明天是雨天,我就 ...
- 自适应 or 移动前端页面布局的问题?
说出来,还请各位看官不要笑 - -.哥们至今不是很清楚页面的自适应和移动前端页面开发有什么区别 .有幸哪位看官清楚这些的话,希望能不吝赐教 .A.meta <meta name=& ...
- /dev/socket/vold exploit 本地提权漏洞
EXPLOIT "0 asec create ../../../../../../../../xxxxx/xx/xx/xx 1 ext4 98235792350852308254872354 ...
- android dom方式创建xml
http://blog.csdn.net/nxh_love/article/details/7085174 在android dom 解析xml方式文章中,简单介绍了dom解析xml的应用.今天在原文 ...