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

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

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

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

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

  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已经实现了对实时文件夹的相关支持

  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"); @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;

  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 { @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();
}
}
  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>
<?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>  

Android 实时文件夹的更多相关文章

  1. android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位   public class FileSizeUtil { public static final int SIZETYPE_B ...

  2. 使用实时文件夹显示ContentProvider的数据

    所谓实时文件夹(即LiveFolder),是指用于显示ContentProvider提供的数据的桌面组件. ContentProvider用于向外提供数据访问的接口,一个应用程序可通过ContentP ...

  3. (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...

  4. android assets文件夹浅谈

    ---恢复内容开始--- 最近在研究assets文件夹的一些属性跟使用方法.根据网上一些文章.实例做一下汇总,拿出来跟大家分享下,有不足的地方还请多多指教. 首先了解一下assets是干什么用的,as ...

  5. Android 建立文件夹、生成文件并写入文本文件内容

    一.首先添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">& ...

  6. Android布局文件夹引起的问题

    Android 运行到setContentView(R.layout.splash); 总是出现如下的错误: java.lang.RuntimeException: Unable to start a ...

  7. 分析cocos2d-x在Android上的编译过程(1):cocco2d-x是怎样生成的Android的文件夹结构

    当新建完一个cocos2d-x的项目后.进入到项目中的proj.android中,会看到例如以下的文件夹结构 在VS先把它编译,然后导入到Eclipse中,导入完后会看到多了几个文件 watermar ...

  8. Android assets文件夹之位置放置和作用

    Android 的assets文件夹的放置位置,Eclipse创建项目时就生成了的,Android Studio则不太一样,AS可以包含几种方式, 1:可以在build.gradle文件下配置,加如下 ...

  9. android project 文件夹

    android多国语言文件夹 http://www.blogjava.net/zhaojianhua/archive/2012/02/09/369676.html Android平板开发精确适配不同的 ...

随机推荐

  1. saltstack对递归依赖条件(死循环依赖)的处理

    本文将对saltstack中状态文件中require条件产生死循环的情形进行简单的测试和分析 测试思路: 写一个包含递归依赖条件的状态文件,进行测试:      A依赖于B      B依赖于C    ...

  2. this——笔记

    this是执行上下文中的一个属性.this与上下文中可执行代码的类型有直接关系,this值在进入上下文时确定,并且在上下文运行期间永久不变. 在这里一切都简单.在全局代码中,this始终是全局对象本身 ...

  3. HTML5和CSS3实例教程[总结二]

    基于contenteditable属性实现在位编辑 HTML5规范引入了contenteditable属性,它几乎可以用在任何元素上,只要添加这一属性 即可变为可编译区域 <!DOCTYPE h ...

  4. 如何在sqlserver建立新用户并关联相应的数据库

    我们经常需要在数据库上建立有权限的用户,该用户只能去操作某个特定的数据库(比如该用户只能去读,去写等等),那么我们应该怎么在sqlserver上设置呢?下面的步骤有点长,只要一步一步跟着设置就行 方法 ...

  5. DropDownList获取的SelectIndex一直为0

    1.想要DropDownList自动提交必须设置AutoPostBack="true"属性,下面是代码: <asp:DropDownList ID=" AutoPo ...

  6. hadoop文本转换为序列文件

    在以前使用hadoop的时候因为mahout里面很多都要求输入文件时序列文件,所以涉及到把文本文件转换为序列文件或者序列文件转为文本文件(因为当时要分析mahout的源码,所以就要看到它的输入文件是什 ...

  7. MySQL 聚簇索引

    聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...

  8. 设置iOS项目BuildVersion自动增加-备用

    一.概念阐述:Build与Version的区别 在iOS中有两种“版本号”,也就是所谓的version号与build号,如下图所示: 我们用最简洁的语言来区分这两个版本号的区别以及用途如下: Vers ...

  9. Powershell Switch 条件

    Powershell Switch 条件 6 21 1月, 2012  在 Powershell tagged Powershell教程/ 分支/ 字符串/ 数字/ 条件by Mooser Lee 本 ...

  10. 关于API的设计和需求抽象

    一,先来谈抽象吧,因为抽象跟后面的API的设计是息息相关的 有句话说的好(不知道谁说的了):计算机科学中的任何问题都可以抽象出一个中间层就解决了. 抽象是指在思维中对同类事物去除其现象的.次要的方面, ...