Read Contacts in Android Mobile Application.
Step 1:-
MainActivity.java
package com.example.readcontacts;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
readAllContacts();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void readAllContacts() throws IOException {
String[] contacts = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
String name = null;
String phonenumber = null;
TextView txt = (TextView) findViewById(R.id.readcontact_results);
Cursor cursorphoneContacts = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contacts,
null, null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " Asc");
try {
} catch (CursorIndexOutOfBoundsException o) {
System.out.println("Error" + o);
}
if (cursorphoneContacts != null && cursorphoneContacts.getCount() > 0) {
cursorphoneContacts.moveToFirst();
do {
name = cursorphoneContacts.getString(cursorphoneContacts
.getColumnIndex(Contacts.DISPLAY_NAME));
phonenumber = cursorphoneContacts
.getString(cursorphoneContacts
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
txt.append("\nName-->" + name + "\n");
txt.append("Phone Number-->" + phonenumber + "\n\n\n");
} while (cursorphoneContacts.moveToNext());
cursorphoneContacts.close();
} else {
Toast.makeText(getApplicationContext(), "There is no Contacts...",
Toast.LENGTH_LONG).show();
}
}
}
Step 2:-
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.readcontacts.MainActivity" >
<TextView
android:id="@+id/readcontact_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="74dp"
android:text="TextView" />
</RelativeLayout>
Step 3:-
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Step 4 :-
Screenshot:-
Source Code:-
Reference:
No comments:
Post a Comment