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> 



  

由于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> 

 

 

 

 

 

 

 

  • 大小: 67 KB
  • 大小: 91 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值