Android 实时文件夹

来源:互联网 发布:查微信聊天记录软件 编辑:程序博客网 时间:2024/05/16 06:09

实时文件夹是一种用来显示由某个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> 



  

由于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();    }}

 二、定义支持实时文件夹的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();    }}

 

 

<?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)
  • 下载次数: 26
  • MyAllContacts.rar (23.4 KB)
  • 下载次数: 28
  • 大小: 67 KB
  • 大小: 91 KB
  • 查看图片附件
原创粉丝点击