zoukankan      html  css  js  c++  java
  • Android 实时文件夹

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

    Java代码 复制代码 收藏代码
    1. import android.content.Intent;  
    2. import android.os.Bundle;  
    3. import android.provider.LiveFolders;  
    4.   
    5. public class CreateLiveFolder extends Activity {  
    6.   
    7.     @Override  
    8.     protected void onCreate(Bundle savedInstanceState) {  
    9.         super.onCreate(savedInstanceState);  
    10.   
    11.         if (getIntent().getAction()  
    12.                 .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {  
    13.               
    14.             Intent intent = new Intent();  
    15.   
    16.             intent.setData(CountryCode.LIVE_FOLDER_URI);  
    17.             intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,  
    18.                     "CountryCode");  
    19.             intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,  
    20.                     Intent.ShortcutIconResource.fromContext(this,  
    21.                             R.drawable.icon));  
    22.             intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,   
    23.                     LiveFolders.DISPLAY_MODE_LIST);  
    24.               
    25.             setResult(RESULT_OK, intent);  
    26.         } else {  
    27.             setResult(RESULT_CANCELED);  
    28.         }  
    29.         finish();  
    30.     }  
    31. }  
    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代码 复制代码 收藏代码
    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.               
    17.         <activity android:name=".CreateLiveFolder">  
    18.             <intent-filter>  
    19.                 <action android:name=  
    20.                     "android.intent.action.CREATE_LIVE_FOLDER" />  
    21.                 <category android:name=  
    22.                     "android.intent.category.DEFAULT" />  
    23.             </intent-filter>  
    24.         </activity>  
    25.     </application>  
    26. </manifest>   
  • 相关阅读:
    GDB命令行最基本操作
    mysql待整理
    python生成二维数组
    python2.7执行shell命令
    使用matplot做图--sin图像
    python--Numpy简单实用实例
    python多线程的使用
    pyv8使用总结
    QDialog:输入对话框、颜色对话框、字体对话框、文件对话框
    pyqt重写键盘事件+获取信号发送对象
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/4241899.html
Copyright © 2011-2022 走看看