Android How to - Read contact








The following code shows how to Read contact and get contact information.

Example

manifest xml file

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.manning.androidhacks.hack045"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="preferExternal">

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
</manifest>

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/times_opened"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />

</LinearLayout>

Java code

/*******************************************************************************
 * Copyright (c) 2012 Manning//from   ww w.j  a va 2  s.  c  o m
 * See the file license.txt for copying permission.
 ******************************************************************************/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.SharedPreferences;
import android.util.Log;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

  private static final String PREFS_NAME = "main_activity_prefs";
  private static final String TIMES_OPENED_KEY = "times_opened_key";
  private static final String TIMES_OPENED_FMT = "Times opened: %d";

  private TextView mTextView;
  private int mTimesOpened;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mTextView = (TextView) findViewById(R.id.times_opened);
  }

  @Override
  protected void onResume() {
    super.onResume();

    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
    mTimesOpened = prefs.getInt(TIMES_OPENED_KEY, 1);
    mTextView.setText(String.format(TIMES_OPENED_FMT, mTimesOpened));
  }

  @Override
  protected void onPause() {
    super.onPause();

    Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();
    editor.putInt(TIMES_OPENED_KEY, mTimesOpened + 1);
    SharedPreferencesCompat.apply(editor);
  }
}

/**
 * Reflection utils to call SharedPreferences$Editor.apply when possible,
 * falling back to commit when apply isn't available.
 */
class SharedPreferencesCompat {
  private static final String TAG = SharedPreferencesCompat.class
      .getCanonicalName();
  private static final Method sApplyMethod = findApplyMethod();

  private static Method findApplyMethod() {
    try {
      Class<?> cls = SharedPreferences.Editor.class;
      return cls.getMethod("apply");
    } catch (NoSuchMethodException unused) {
      // fall through
    }
    return null;
  }

  public static void apply(SharedPreferences.Editor editor) {
    if (sApplyMethod != null) {
      try {
        sApplyMethod.invoke(editor);
        Log.d(TAG, "Apply method was invoked!");
        return;
      } catch (InvocationTargetException unused) {
        // fall through
      } catch (IllegalAccessException unused) {
        // fall through
      }
    }
    editor.commit();
    Log.d(TAG, "Commit method was invoked!");
  }
}