Hello friends,
Today we are going to learn about one example. Into this we can fetch all the contact from Addressbook and print log in android Logcat. It's important think in android we can see much more app has this type of contact list. so Let's take an example.
First we need to add one permission in our project AndroidMainfest.xml file.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayContacts();
}
private void displayContacts() {
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor Cur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (Cur.moveToNext()) {
String phoneNomber = Cur.getString(Cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("Contact List","Name: " + name + ", Phone No: " + phoneNomber);
}
Cur.close();
}
}
}
}
}
"Check Your Output in Logcat bottom of the android studio IDE"
Today we are going to learn about one example. Into this we can fetch all the contact from Addressbook and print log in android Logcat. It's important think in android we can see much more app has this type of contact list. so Let's take an example.
First we need to add one permission in our project AndroidMainfest.xml file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayContacts();
}
private void displayContacts() {
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor Cur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (Cur.moveToNext()) {
String phoneNomber = Cur.getString(Cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("Contact List","Name: " + name + ", Phone No: " + phoneNomber);
}
Cur.close();
}
}
}
}
}
"Check Your Output in Logcat bottom of the android studio IDE"
Comments
Post a Comment