Android即時檔案夾建立方法_Android

來源:互聯網
上載者:User

本文執行個體講述了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類

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

資訊清單檔

<?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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.