Tampilkan postingan dengan label Reading contacts from phone book in Android. Tampilkan semua postingan
Tampilkan postingan dengan label Reading contacts from phone book in Android. Tampilkan semua postingan

Jumat, 11 November 2011

Reading Names And Contacts In Android and Storing In Bean

import java.io.InputStream;
import profileit.utils.ContactsBean;
import profileit.utils.StaticUtils;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.WindowManager;

public class Splash extends Activity {
private final int DISPLAY_LENGTH = 2000;
static ContentResolver cr;
static Cursor cursor;
@Override
public void onCreate(Bundle si) {
super.onCreate(si);
// title bar removing
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
// call next activity specified delay time
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// clear utils if necessary
StaticUtils.sALContacts.clear();
StaticUtils.sALNames.clear();

getHome();

}

}, DISPLAY_LENGTH);
}
// gettings Contacts
public void getHome() {

cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);

if (cur.getCount() > 0) {
int count = 0;
while (cur.moveToNext()) {
// created bean for Storing data as a object
ContactsBean bean = new ContactsBean();
// getting contact id as a String
String contid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// getting contact id as a long type
long contactId = cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID));
// Log.e("iteratin started" + ++count, contid);
// fecthing contact name using contact ID allocated by OS
String name = getContacName(contactId);
if (name != null) {
// fecthing contact number using contact ID allocated by OS
String cNumber = getContactNumber(contactId);
if (cNumber != null) {
// Storing number in Bean
bean.setmPhoneNumber(cNumber);
} else {
bean.setmPhoneNumber(StaticUtils.sTstNoNumbMesg);
}
//fecthing Email using contact ID
String cEmail = StaticUtils.getContactEmail(cr, contactId);
if (cEmail != null) {
// Storing Email in Bean
bean.setmEmail(cEmail);
} else {
bean.setmEmail(StaticUtils.sTstNoEmailMesg);
}
//fecthing photo using contact ID, it returns as a stream
InputStream is = StaticUtils.getContactPhoto(cr, contactId);
if (is != null) {
// Storing photo in Bean
bean.setmPhoto(true);
} else {
bean.setmPhoto(!true);
}
if (cNumber != null || cEmail != null || is != null) {
bean.setmContact_Id(contid);
bean.setmName(name);
// storing Bean object into Arraylist
StaticUtils.sALContacts.add(bean);
// storing names in bean
StaticUtils.sALNames.add(name.toLowerCase());
// Log.e("contact name",StaticUtils.sALNames.toString());
// Log.e("contacts",StaticUtils.sALContacts.toString());
} else {
// Log.e("No contact for",name);
}
} else {
// Log.e("contact name may", "be null");
}
// Log.e("iteratin over" + count, contid);
}
}
}

// fecthing name using contact ID
public String getContacName(long contactId) {
// formarting URI for getting name
cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data.DATA1 },
Data.CONTACT_ID + "=" + contactId + " and " + Data.MIMETYPE
+ "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null,
null);
if (cursor == null) {
// Log.e("cursor ContacName", "is null");
return null;
} else {
try {
// Log.e("cursor ContacName", "is not null");
if (cursor.moveToFirst()) {
String data = cursor.getString(0);
if (data != null) {
// Log.e("data ContacName", "obj" + data);
return data;
} else {

// Log.e("data ContacName", "is null");
}
} else {
// Log.e("cursor move first ContacName", "failed");
}
} finally {
cursor.close();
}

}

return null;
}

// fecthing number using contact ID
public String getContactNumber(long contactId) {
// formarting URI for getting name
cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data.DATA1 },
Data.CONTACT_ID + "=" + contactId + " and " + Data.MIMETYPE
+ "='" + Phone.CONTENT_ITEM_TYPE + "'", null, null);
if (cursor == null) {
// Log.e("cursor getPhone", "is null");
return null;
} else {
try {
// Log.e("cursor getPhone", "is not null");
if (cursor.moveToFirst()) {
String data = cursor.getString(0);
if (data != null) {
// Log.e("data getPhone", "obj" + data);
return data;
} else {
// Log.e("data getPhone", "is null");
}
} else {
// Log.e("cursor move first getPhone", "failed");
}
} finally {
cursor.close();
}
}
return null;

}

}

Kamis, 10 November 2011

How to call Android contacts list?

Hi this is very import concept to Read Existing Contacts from Your Emulator or Device

There are three steps to this process.

1) Permissions

Add a permission to read contacts data to your application manifest.

 android:name="android.permission.READ_CONTACTS"/>

2) Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACTin this example). This will cause Android to launch an Activity that's registered to support ACTION_PICKon the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override
public vo
id onActivityResult(int reqCode, int resultCode, Intent data) {
sup
er.onA


ctivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}

Home Screen If No Contacts Contacts










For Online Training :

For Full Source Cliked Here