Android實(shí)時(shí)文件夾創(chuàng)建方法
本文實(shí)例講述了Android實(shí)時(shí)文件夾創(chuàng)建方法。分享給大家供大家參考。具體如下:
實(shí)時(shí)文件夾是一種用來顯示由某個(gè)ContentProvider提供的數(shù)據(jù)信息的桌面組件。要?jiǎng)?chuàng)建一個(gè)實(shí)時(shí)文件夾,必須要有兩方面的支持。一方面是,要定義一個(gè)用來創(chuàng)建實(shí)時(shí)文件夾的Activity。另一方面是,所指定數(shù)據(jù)信息URI的ContentProvider必須支持實(shí)時(shí)文件夾的查詢。本節(jié)中就將要介紹如何為應(yīng)用程序創(chuàng)建實(shí)時(shí)文件夾。
與在Launcher的桌面上添加一個(gè)快捷方式類似,用戶在桌面上長按后選擇實(shí)時(shí)文件夾就會(huì)彈出一個(gè)可用實(shí)時(shí)文件夾的列表對話框。若我們想把自己應(yīng)用程序內(nèi)的Activity也添加到這一列表中,同樣只需要在該Activity注冊時(shí)添加一個(gè)Action為android.intent.action.CREATE_LIVE_FOLDER的IntentFilter。而在這個(gè)創(chuàng)建實(shí)時(shí)文件夾的Activity中,我們要把實(shí)時(shí)文件夾的信息以附加信息的形式存儲(chǔ)在一個(gè)Intent對象當(dāng)中,并通過Result返回給Launcher應(yīng)用程序執(zhí)行添加。下表列出了與實(shí)時(shí)文件夾信息相關(guān)的附件信息的鍵值與數(shù)據(jù)類型。
實(shí)時(shí)文件夾的鍵值與數(shù)據(jù)類型

其中DISPLAY_MODE有兩種,其值為1時(shí),以柵格(Grid)形式顯示展開后的實(shí)時(shí)文件夾內(nèi)容,為2時(shí)則是以列表(List)形式顯示。除了以上的附加信息,對于要查詢數(shù)據(jù)的URI則是以Data的形式存儲(chǔ)在Intent對象中的。由于Contacts的ContentProvider已經(jīng)實(shí)現(xiàn)了對實(shí)時(shí)文件夾的相關(guān)支持,所以下面我們就以創(chuàng)建所有聯(lián)系人的實(shí)時(shí)文件夾的程序來作為本節(jié)的示例。
TestActivity類
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, "電話本"); //快捷方式的標(biāo)題
intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
Intent.ShortcutIconResource.fromContext(this, R.drawable.png1));//快捷方式的圖標(biāo)
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.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>
運(yùn)行結(jié)果



希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android利用WindowManager生成懸浮按鈕及懸浮菜單
這篇文章主要為大家詳細(xì)介紹了Android利用WindowManager生成懸浮按鈕及懸浮菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
解決NDK開發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終解決方法(已測)
這篇文章主要介紹了解決NDK開發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終方法,需要的朋友可以參考下2016-12-12
Android 使用selector改變按鈕狀態(tài)實(shí)例詳解
這篇文章主要介紹了Android 使用selector改變按鈕狀態(tài)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android通過代碼控制ListView上下滾動(dòng)的方法
今天小編就為大家分享一篇關(guān)于Android通過代碼控制ListView上下滾動(dòng)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

