Home > Android | ケータイ | 開発 > [Android] ContentProvider を使って コンタクトリストのデータを取得する

[Android] ContentProvider を使って コンタクトリストのデータを取得する

Android の ContentProvider クラス について。

チュートリアルにある NotePad アプリケーションではデータの保存に SQLite を使っていますが、

For this exercise, we are just going to use a SQLite database directly to store our data, but in a real application it would be much better to write a proper ContentProvider to encapsulate this behavior instead.

If you are interested, you can find out more about content providers or the whole subject of Storing, Retrieving, and Exposing Data.


とあるように、実際アプリケーションを作る場合は ContentProvider を使う方がいいようです。

というわけで、以下のURL を参考に ContentProvider を使ってみました。
http://code.google.com/android/devel/data/contentproviders.html

まずは、サンプルコードにあるように、アドレス帳のデータを取り出してリスト形式で表示してみることにします。

1. 新しい Android プロジェクトを作る。

プロジェクト名、アプリケーション名は ContactView にしました。

2. layout 用の xml ファイルを作成。

main.xml に ListView を追加
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView id="@+id/android:list"
          android:layout_width="wrap_content"
        	android:layout_height="wrap_content"/>

  	<TextView id="@+id/android:empty"
          android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="No Contacts!"/>
</LinearLayout>

リスト内の表示用に、contacts_row.xml を追加
<?xml version="1.0" encoding="utf-8"?>
<TextView id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

3. コンタクトリストからデータを表示するプログラムを書きます。

package jp.plantsweb.android.contact;

import java.util.ArrayList;

import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ContactList extends ListActivity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        fillData();
    }

    /**
     * コンタクトデータを取得して表示
     */
    private void fillData(){
        List items = new ArrayList();

	     // コンタクトリストから取得するカラムを指定
	     String[] projection = new String[] {
   		    android.provider.BaseColumns._ID,
       		android.provider.Contacts.PeopleColumns.NAME
    	};
	     

	    // クエリを発行する
    	Cursor cur = managedQuery( android.provider.Contacts.Phones.CONTENT_URI,
                 projection, // 取得するカラムを指定
               	 null,             // WHERE句にあたる部分。全部必要なのでnull
                 android.provider.Contacts.PeopleColumns.NAME + " ASC"); // Order by

       	String name;

       	// データ取得用のインデックスを取得する
      	int nameColumn = cur.getColumnIndex(android.provider.Contacts.PeopleColumns.NAME); 
       	// データ取得開始
		while (cur.next()) {
  	        	// データ取得
      	    	name = cur.getString(nameColumn);

	      	    // 表示用の配列に追加
				items.add(name);
		}
     	// アダプタを介してデータ表示
       	ArrayAdapter contacts = new ArrayAdapter(

       		this, R.layout.contacts_row, items);
       	setListAdapter(contacts);
    }
}

Run してみたところ、SecurityException が発生しました。

error

デフォルトではコンタクトリストには許可されたアプリケーションしかアクセスできない模様です。

4. パーミッションの設定をする

セキュリティの章に答えがありました。 http://code.google.com/android/devel/security.html

保護されているデータにアクセスする場合、AndroidManifest.xml に、
<uses-permission id="android.permission.RECEIVE_SMS" />
というような宣言を書く必要があるようです。

パーミッションの一覧が以下の URL にあり、
http://code.google.com/android/reference/android/Manifest.permission.html
READ_CONTACT というパーミッションがありました。AndoroidManifest.xml に

<uses-permission id="android.permission.READ_CONTACTS" /> 

という行を追加して RUN してみたところ、無事コンタクトリストの内容が表示されました!


list of address

しかし、コンタクトリストには2名しか登録していないはずですが、4行になっていますね。調べてみたところ、ContactList 内のデータは、携帯と自宅と登録されている場合は2行分に分かれているようです。

次回はオリジナルの ContentProvider を作ってみようと思います。

Comment:1

No Name 2010-09-15 (水) 06:07

안녕하세요 글 잘 보고 갑니다 ^^

Comment Form
Remember personal info

Trackback:2

TrackBack URL for this entry
http://www.plants-web.jp/mt/mt-tb.cgi/233
Listed below are links to weblogs that reference
[Android] ContentProvider を使って コンタクトリストのデータを取得する from Flashマインドマップ開発ブログ
[Android]Activity の Permission 一覧 from Raspberry Farad 2009-05-06 (水) 02:41
ほとんどの Action Intent は、実行前に適切な Permission 設定が必要である。 Activity に Permission を設定...
[Android]Activity の Permission 一覧 from Raspberry Farad 2009-05-06 (水) 02:48
ほとんどの Action Intent は、実行前に適切な Permission 設定が必要である。 Activity に Permission を設定...

Home > Android | ケータイ | 開発 > [Android] ContentProvider を使って コンタクトリストのデータを取得する

Search
Feeds

follow hal_sk at http://twitter.com

Page Top