实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件。要创建一个实时文件夹,必须要有两个方面的支持。

1,要定义一个用来创建实时文件夹的Activity。

2,所指定数据信息URI的ContentProvider必须支持实时文件夹时文件夹查询

一、定义创建实时文件夹的Activity

想在桌面长按后选择实时文件夹就会弹出一个可用实时文件夹的列表对话框,必须在应用程序内的Activity中添加一个Action为android.intent.action.CREATE_LIVE_FOLDER的IntentFilter。而在这个创建实时文件夹的Activity中,我们要把实时文件夹的信息以附加信息的形式存储在一个Intent对象当中。

Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.studio.android.ch10.ex2"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon"
  7. android:label="@string/app_name">
  8. <activity android:name=".MyAllContacts"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name=
  12. "android.intent.action.CREATE_LIVE_FOLDER" />
  13. <category android:name=
  14. "android.intent.category.DEFAULT" />
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. <uses-sdk android:minSdkVersion="3" />
  19. </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已经实现了对实时文件夹的相关支持

Java代码  
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.provider.Contacts;
  6. import android.provider.LiveFolders;
  7. public class MyAllContacts extends Activity {
  8. public static final Uri LIVE_FOLDER_URI =
  9. Uri.parse("content://contacts/live_folders/people");
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. if (getIntent().getAction()
  14. .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
  15. Intent intent = new Intent();
  16. intent.setData(LIVE_FOLDER_URI);//在文件夹,对于要查询的URI则是以Data的形式存储在Intent对象中。Contacts的ContentProvider已经实现了对实时文件夹的相关支持。
  17. intent.putExtra(
  18. LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT,
  19. new Intent(Intent.ACTION_VIEW,
  20. Contacts.People.CONTENT_URI));
  21. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
  22. "MyAllContacts");
  23. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
  24. Intent.ShortcutIconResource.fromContext(this,
  25. R.drawable.icon));
  26. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
  27. LiveFolders.DISPLAY_MODE_LIST);//还可以设置LiveFolders.DISPLAY_MODE_LIST
  28. setResult(RESULT_OK, intent);
  29. } else {
  30. setResult(RESULT_CANCELED);
  31. }
  32. finish();
  33. }
  34. }
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");@Overrideprotected 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_LISTsetResult(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;

Java代码  
  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.provider.LiveFolders;
  4. public class CreateLiveFolder extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. if (getIntent().getAction()
  9. .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
  10. Intent intent = new Intent();
  11. intent.setData(CountryCode.LIVE_FOLDER_URI);
  12. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
  13. "CountryCode");
  14. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
  15. Intent.ShortcutIconResource.fromContext(this,
  16. R.drawable.icon));
  17. intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
  18. LiveFolders.DISPLAY_MODE_LIST);
  19. setResult(RESULT_OK, intent);
  20. } else {
  21. setResult(RESULT_CANCELED);
  22. }
  23. finish();
  24. }
  25. }
import android.content.Intent;
import android.os.Bundle;
import android.provider.LiveFolders;public class CreateLiveFolder extends Activity {@Overrideprotected 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代码  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.studio.android.chp10.ex3"
  4. android:versionCode="1"
  5. android:versionName="1.0.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".SQLite2"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <provider android:name="MyProvider"
  15. android:authorities="com.studio.andriod.provider.countrycode" />
  16. <activity android:name=".CreateLiveFolder">
  17. <intent-filter>
  18. <action android:name=
  19. "android.intent.action.CREATE_LIVE_FOLDER" />
  20. <category android:name=
  21. "android.intent.category.DEFAULT" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </manifest>

转载于:https://www.cnblogs.com/xiaochao1234/p/4241899.html

Android 实时文件夹相关推荐

  1. Android桌面组件开发之实时文件夹(Live_Folders)

    转自:http://zhangkun716717-126-com.iteye.com/blog/813059 实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件.要创 ...

  2. android 获取文件夹下的所有文件

    昨天,在做工作时,需要遍历所有一个文件夹下的所有文件夹,当时自己也不知道怎么做,后来在网上搜索了一些资料,发现其实也很简单. 1.获取SD是否可以读写,如果可以,则传入文件的路径 /*读取输入的某个文 ...

  3. android 删除目录下所有文件大小,Android 删除文件夹(文件夹以及文件夹下所有的文件)、文件...

    1.Android 删除文件夹(文件夹以及文件夹下所有的文件) //删除文件夹和文件夹里面的文件 public static void deleteDirWihtFile(File dir) { if ...

  4. android data文件夹操作

    1,看代码 public class DBTest extends Activity {SQLiteDatabase db;Button bn = null;ListView listView;@Ov ...

  5. android sdk文件位置,Android SDK文件夹位于何处?

    我通过Air for Android用Adobe Flash创建了一个.apk应用程序.现在,我想通过这款Blackberry在线打包机为黑莓App World做好准备:https://bdsc.we ...

  6. Android获取文件夹路径

    Android获取文件夹路径 应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的. 大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中. ...

  7. Android各个文件夹对应的分辨率?

    #一. 描述 各个分辨率对应Android的图片文件夹? #二.对应分辨率 转载于:https://juejin.im/post/5b3ac699e51d4555687ab6b0

  8. android 浏览文件夹,如何浏览android中的文件夹并获取所选文件夹的路径

    当我单击一个按钮,显示一个文件浏览器时,我可以选择一个文件夹并返回它的路径.我得到这个路径将文件复制到该路径. 但我不知道如何实现这一点. 我还在Stackoverflow中寻找这个问题,但我没有找到 ...

  9. android camera 检测,检测Android Camera文件夹

    我想检测每个 Android设备上的相机文件夹.据我所知,这个文件夹与制造商不同,并且无法保证设备上甚至还有一个DCIM文件夹. 这是我现在用来获取文件的方法: private static fina ...

最新文章

  1. 2022-2028年中国玫瑰花行业市场研究及前瞻分析报告
  2. CRM 客户端程序开发:获取表单界面上各种字段的值及其他属性
  3. axios create拦截_Vue学习-axios
  4. html按钮按下效果_CSS+HTMLlt;水滴按钮效果gt;
  5. spring4.x(6)---SpringIOC的Scope配置
  6. 点阵字体显示系列补记:将字库文件转换成数组形式
  7. Python组合数据类型:容器类型转换,list←→tuple←→set
  8. python commands_python之commands模块
  9. SQL Server删除重复行的6个方法
  10. arrayvalue php,phparrayvalue
  11. 设置Response中的ContentType
  12. t470键盘拆解_Thinkpad 二手T470笔记本拆解|支持双硬盘|拆机教程
  13. 六、矩阵键盘的扫描原理与基本应用
  14. Android Studio开发记录
  15. iPhone没有声音,但插上耳机后声音正常的处理
  16. 2017年大工考博英语加试试题。给学弟学妹们一点帮助
  17. django的admin站点生成超级用户出错
  18. java:常见的日期转型,“Tue Oct 18 00:00:00 CST 2022“
  19. 动漫人物头发怎么画(头发画法步骤)
  20. PhysicalDrive

热门文章

  1. Java使用字节码和汇编语言同步分析volatile,synchronized的底层实现
  2. Java json转Map,转bean,转Listbean
  3. Ubuntu14.04上编译指定版本的protobuf源码操作步骤
  4. 用vs2010编译vigra静态库及简单使用举例
  5. Windows XP下vs2010中配置OpenCV2.4.3
  6. 【linux】用户和组的管理:添加、修改、删除(useradd usermod userdel groupadd groupdel)
  7. 【POCO】POCO学习总结(四)——MinGW编译poco
  8. 文件名转换为utf8 c语言,文件名编码转换:从 gb* 转向 utf8 必备工具 convmv
  9. php 单一入口 seo,网站结构分类(单一入口还是多入口)
  10. 鱼眼相机标定_鱼眼相机模型(二)