本文實例講述了android實時文件夾創建方法。分享給大家供大家參考。具體如下:
實時文件夾是一種用來顯示由某個contentprovider提供的數據信息的桌面組件。要創建一個實時文件夾,必須要有兩方面的支持。一方面是,要定義一個用來創建實時文件夾的activity。另一方面是,所指定數據信息uri的contentprovider必須支持實時文件夾的查詢。本節中就將要介紹如何為應用程序創建實時文件夾。
與在launcher的桌面上添加一個快捷方式類似,用戶在桌面上長按后選擇實時文件夾就會彈出一個可用實時文件夾的列表對話框。若我們想把自己應用程序內的activity也添加到這一列表中,同樣只需要在該activity注冊時添加一個action為android.intent.action.create_live_folder的intentfilter。而在這個創建實時文件夾的activity中,我們要把實時文件夾的信息以附加信息的形式存儲在一個intent對象當中,并通過result返回給launcher應用程序執行添加。下表列出了與實時文件夾信息相關的附件信息的鍵值與數據類型。
實時文件夾的鍵值與數據類型
其中display_mode有兩種,其值為1時,以柵格(grid)形式顯示展開后的實時文件夾內容,為2時則是以列表(list)形式顯示。除了以上的附加信息,對于要查詢數據的uri則是以data的形式存儲在intent對象中的。由于contacts的contentprovider已經實現了對實時文件夾的相關支持,所以下面我們就以創建所有聯系人的實時文件夾的程序來作為本節的示例。
testactivity類
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.ljq.activity; import android.app.activity; import android.content.intent; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.provider.livefolders; public class testactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); if (getintent().getaction().equals(livefolders.action_create_live_folder)){ intent intent = new intent(); intent.setdata(uri.parse( " content://contacts/live_folders/people " )); intent.putextra(livefolders.extra_live_folder_base_intent, new intent(intent.action_view,contactscontract.contacts.content_uri)); intent.putextra(livefolders.extra_live_folder_name, "電話本" ); //快捷方式的標題 intent.putextra(livefolders.extra_live_folder_icon, intent.shortcuticonresource.fromcontext( this , r.drawable.png1)); //快捷方式的圖標 intent.putextra(livefolders.extra_live_folder_display_mode, livefolders.display_mode_list); //顯示模型 setresult(result_ok, intent); } else { setresult(result_canceled); } finish(); } } |
清單文件
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= " http://schemas.android.com/apk/res/android " package = "com.ljq.activity" android:versioncode= "1" android:versionname= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ".testactivity" 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= "7" /> </manifest> |
運行結果
希望本文所述對大家的android程序設計有所幫助。