Tampilkan postingan dengan label Emulator. Tampilkan semua postingan
Tampilkan postingan dengan label Emulator. 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;

}

}