SlideShare a Scribd company logo
Content Providers.
Step 1. Create a new Android Project of blank Activity.
Step 2. Add the following code to your
layout/layout_activity.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/contactview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Step3.
Access to thecontact ContentProvider requires a certain permission, as not all
applications should haveaccess to thecontact information. Open
the AndroidManifest.xml file, and select thePermissions tab. On that tab click
the Add button, and select the Uses Permission. From thedrop-down list select
the android.permission.READ_CONTACTS entry
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
Step 4. Add the following code on the“oncreate” method of your activity
TextView contactView = (TextView)
findViewById(R.id.contactview);
Step5. Addthe followingfunction.
private void getContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.READ_CONTACTS) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 100);
//After this point you wait for callback in onRequestPermissionsResult(int,
String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
// display in the list view
contactView.setText(name);
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =
?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
}
}
}
}
}
6. Use ListViewinsteadof TextViewto displaycontacts

More Related Content

PDF
Android Feature Sprint
DOCX
4.preference management
PDF
Hierarchy viewer
PPTX
Top 20 Reasons your App can be banned on Play Store
PPTX
Build Your First Android App
PPTX
Measuring the Right App Metrics - Guide for Beginners
PDF
Your app may be blocked soon and 5 essential things that no one is telling yo...
PDF
Tools to make killer apps: Quick guide for developers
Android Feature Sprint
4.preference management
Hierarchy viewer
Top 20 Reasons your App can be banned on Play Store
Build Your First Android App
Measuring the Right App Metrics - Guide for Beginners
Your app may be blocked soon and 5 essential things that no one is telling yo...
Tools to make killer apps: Quick guide for developers

Similar to 5. content providers (20)

PPTX
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
PPTX
Android Marshmallow APIs and Changes
PPTX
Lecture #1 Creating your first android project
PDF
Android tutorial
DOCX
Creation of simple application using - step by step
PDF
Android app development guide for freshers by ace web academy
PDF
Mobile Application Development -Lecture 09 & 10.pdf
PPT
Beginning Native Android Apps
PDF
Android session 2
PPTX
Introduction to Android Programming
DOCX
Lecture exercise on activities
DOCX
Leture5 exercise onactivities
PDF
Android Development: Build Android App from Scratch
PDF
Android Basic- CMC
PPTX
Android Apps Development Basic
PPT
Best android classes in mumbai
PDF
Google Cloud Messaging
PDF
Android Minnebar
PPTX
Android Tutorials : Basic widgets
PDF
Android Components & Manifest
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Android Marshmallow APIs and Changes
Lecture #1 Creating your first android project
Android tutorial
Creation of simple application using - step by step
Android app development guide for freshers by ace web academy
Mobile Application Development -Lecture 09 & 10.pdf
Beginning Native Android Apps
Android session 2
Introduction to Android Programming
Lecture exercise on activities
Leture5 exercise onactivities
Android Development: Build Android App from Scratch
Android Basic- CMC
Android Apps Development Basic
Best android classes in mumbai
Google Cloud Messaging
Android Minnebar
Android Tutorials : Basic widgets
Android Components & Manifest
Ad

More from maamir farooq (20)

DOCX
Ooad lab1
PPT
Lesson 03
PPT
Lesson 02
PDF
Php client libray
PDF
Swiftmailer
PDF
PDF
PPTX
PDF
PDF
J query 1.7 cheat sheet
PDF
Assignment
PDF
Java script summary
PDF
PDF
PPTX
PPTX
Css summary
DOCX
Manual of image processing lab
PDF
Session management
PDF
Data management
PPTX
Content provider
Ooad lab1
Lesson 03
Lesson 02
Php client libray
Swiftmailer
J query 1.7 cheat sheet
Assignment
Java script summary
Css summary
Manual of image processing lab
Session management
Data management
Content provider
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
Microbial diseases, their pathogenesis and prophylaxis
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Classroom Observation Tools for Teachers
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
Cell Structure & Organelles in detailed.

5. content providers

  • 1. Content Providers. Step 1. Create a new Android Project of blank Activity. Step 2. Add the following code to your layout/layout_activity.xml. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/contactview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> Step3. Access to thecontact ContentProvider requires a certain permission, as not all applications should haveaccess to thecontact information. Open the AndroidManifest.xml file, and select thePermissions tab. On that tab click the Add button, and select the Uses Permission. From thedrop-down list select the android.permission.READ_CONTACTS entry <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> Step 4. Add the following code on the“oncreate” method of your activity TextView contactView = (TextView) findViewById(R.id.contactview);
  • 2. Step5. Addthe followingfunction. private void getContacts() { // Check the SDK version and whether the permission is already granted or not. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 100); //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method } else { // Android version is lesser than 6.0 or the permission is already granted. ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); // display in the list view contactView.setText(name); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); } pCur.close(); } } } } } 6. Use ListViewinsteadof TextViewto displaycontacts