SlideShare a Scribd company logo
Content Providers
1
Sourabh Sahu
ContentProvider
• Sharing Data between Different Apps
Content URIs
• To Query, we have to specify the query
string in the form of a URI which has
following format −
• <prefix>://<authority>/<data_type>/<id>
• Example content://contacts/people/5
• This is used to get the 5th
contact
from contact list
What you can do with
ContentProvider
How to Access Content Provider
• / Queries the user dictionary and returns results
• mCursor = getContentResolver().query(
• CONTENT_URI, // The content URI
to access table
• mProjection, // The columns to
return for each row
• mSelectionClause // Selection criteria
• mSelectionArgs, // Selection criteria
• mSortOrder); // The sort order for
the returned rows
Required Permissions
•   <uses-permission 
android:name="android.permission.READ_
CONTACTS">
• </uses-permission>
• // A "projection" defines the columns that will be 
returned for each row
•  String[] mProjection =
•  {
•      // ContactsContract data table data1 column 
name
•      ContactsContract.CommonDataKinds.Phone.N
UMBER        
•  };
•  
Example
•   /*
•   * This defines a one-element String array 
to contain the selection argument.
•   */
•  String[] mSelectionArgs = {""};
•   
•   
• // Remember to insert code here to check 
for invalid or malicious input.
•   
•  // Constructs a selection clause that 
matches the contact_id from data table.
•  mSelectionClause = 
ContactsContract.CommonDataKinds.Phon
e.CONTACT_ID + " = ?";
• // Get data for 1st record
•  mSelectionArgs[0] = 1;
•  // Does a query against the table and returns a Cursor 
object
•  mCursor = getContentResolver().query( 
•      ContactsContract.CommonDataKinds.Phone.CONTENT_
URI, // URI
•      mProjection,                 // The columns to return for 
each row
•      mSelectionClause             // Data for first contact
•      mSelectionArgs,              // first contact
•      mSortOrder                   // The sort order for the 
returned rows
•            );                 
•   
•  
• // Some providers return null if an error occurs, others throw an
exception
• if (null == mCursor) {
• // Insert code here to handle the error. Be sure not to use the
cursor! You may want to
• // call android.util.Log.e() to log this error.
• // If the Cursor is empty, the provider found no matches
•
• } else if (mCursor.getCount() < 1) {
• // Insert code here to notify the user that the contact query
was unsuccessful. This isn’t necessarily
• // an error. You may want to offer the user the option to insert
a new row, or re-type the
• // search term.
• } else {
•
• // Insert code here to do something with the results
•
• // Moves to the next row in the cursor. Before the first
movement in the cursor, the
• // "row pointer" is -1, and if you try to retrieve data at that
position you will get an
• // exception.
•
•
• while (mCursor.moveToNext()) {
•
• // Gets the value from the column.
•
• phoneNumber = mCursor.getString(index);
•
• // Show phone number in Logcat
• Log.i("Phone"," Numbers : " + phoneNumber);
•
• // end of while loop
• }
•
• }
PERMISSION
• This will not work in android 6.0 and
above
• You have to explicitly request for
permissions
Check Whether contact read
permission is there or not
• // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
• //Not having permission
• }
• Else {
• //having permission
• }
If Not Requestion Permission
• ActivityCompat.requestPermissions(this,
new String[]
{Manifest.permission.READ_CONTACTS}
,
MY_PERMISSIONS_REQUEST_READ_C
ONTACTS);
Handle permission
• @Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[]
grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
• //Read Cursor Now
• }
• }
Thank You
18

More Related Content

Similar to Content Providers in Android

ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
SCWCD : The servlet model : CHAP : 2
SCWCD  : The servlet model : CHAP : 2SCWCD  : The servlet model : CHAP : 2
SCWCD : The servlet model : CHAP : 2
Ben Abdallah Helmi
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
Sunil Komarapu
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
F K
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
AbdulImrankhan7
 
Junit in mule demo
Junit in mule demo Junit in mule demo
Junit in mule demo
javeed_mhd
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2
Ben Abdallah Helmi
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
SURBHI SAROHA
 
OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)
Jason Huynh
 
14method in c#
14method in c#14method in c#
14method in c#
Sireesh K
 
SQLite Opening .pptx
SQLite Opening .pptxSQLite Opening .pptx
SQLite Opening .pptx
ImranS18
 
Day 6.pptx
Day 6.pptxDay 6.pptx
Day 6.pptx
atreesgalaxy
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
PRITI TELMORE
 
Apache Solr crash course
Apache Solr crash courseApache Solr crash course
Apache Solr crash course
Tommaso Teofili
 
7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 2)7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 2)
Sam Bowne
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Enjoy Munit with Mule
Enjoy Munit with MuleEnjoy Munit with Mule
Enjoy Munit with Mule
Bui Kiet
 
Kelis king - introduction to software design
Kelis king -  introduction to software designKelis king -  introduction to software design
Kelis king - introduction to software design
KelisKing
 
Design API using RAML - basics
Design API using RAML - basicsDesign API using RAML - basics
Design API using RAML - basics
kunal vishe
 

Similar to Content Providers in Android (20)

ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
SCWCD : The servlet model : CHAP : 2
SCWCD  : The servlet model : CHAP : 2SCWCD  : The servlet model : CHAP : 2
SCWCD : The servlet model : CHAP : 2
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
 
Junit in mule
Junit in muleJunit in mule
Junit in mule
 
Junit in mule demo
Junit in mule demo Junit in mule demo
Junit in mule demo
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)OQL querying and indexes with Apache Geode (incubating)
OQL querying and indexes with Apache Geode (incubating)
 
14method in c#
14method in c#14method in c#
14method in c#
 
SQLite Opening .pptx
SQLite Opening .pptxSQLite Opening .pptx
SQLite Opening .pptx
 
Day 6.pptx
Day 6.pptxDay 6.pptx
Day 6.pptx
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Apache Solr crash course
Apache Solr crash courseApache Solr crash course
Apache Solr crash course
 
7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 2)7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 2)
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Enjoy Munit with Mule
Enjoy Munit with MuleEnjoy Munit with Mule
Enjoy Munit with Mule
 
Kelis king - introduction to software design
Kelis king -  introduction to software designKelis king -  introduction to software design
Kelis king - introduction to software design
 
Design API using RAML - basics
Design API using RAML - basicsDesign API using RAML - basics
Design API using RAML - basics
 

More from Sourabh Sahu

Understanding GIT and Version Control
Understanding GIT and Version ControlUnderstanding GIT and Version Control
Understanding GIT and Version Control
Sourabh Sahu
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
Sourabh Sahu
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick Guide
Sourabh Sahu
 
Python Course
Python CoursePython Course
Python Course
Sourabh Sahu
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
Sourabh Sahu
 
SeekBar in Android
SeekBar in AndroidSeekBar in Android
SeekBar in Android
Sourabh Sahu
 
Android layouts
Android layoutsAndroid layouts
Android layouts
Sourabh Sahu
 
Android ListView and Custom ListView
Android ListView and Custom ListView Android ListView and Custom ListView
Android ListView and Custom ListView
Sourabh Sahu
 
Activities
ActivitiesActivities
Activities
Sourabh Sahu
 
Android project architecture
Android project architectureAndroid project architecture
Android project architecture
Sourabh Sahu
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
Sourabh Sahu
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
Sourabh Sahu
 
Calendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePickerCalendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePicker
Sourabh Sahu
 
Progress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialogProgress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialog
Sourabh Sahu
 
AutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextViewAutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextView
Sourabh Sahu
 
Web view
Web viewWeb view
Web view
Sourabh Sahu
 
Parceable serializable
Parceable serializableParceable serializable
Parceable serializable
Sourabh Sahu
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
Sourabh Sahu
 
Android Installation Testing
Android Installation TestingAndroid Installation Testing
Android Installation Testing
Sourabh Sahu
 
Android Installation
Android Installation Android Installation
Android Installation
Sourabh Sahu
 

More from Sourabh Sahu (20)

Understanding GIT and Version Control
Understanding GIT and Version ControlUnderstanding GIT and Version Control
Understanding GIT and Version Control
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick Guide
 
Python Course
Python CoursePython Course
Python Course
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
 
SeekBar in Android
SeekBar in AndroidSeekBar in Android
SeekBar in Android
 
Android layouts
Android layoutsAndroid layouts
Android layouts
 
Android ListView and Custom ListView
Android ListView and Custom ListView Android ListView and Custom ListView
Android ListView and Custom ListView
 
Activities
ActivitiesActivities
Activities
 
Android project architecture
Android project architectureAndroid project architecture
Android project architecture
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Calendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePickerCalendar, Clocks, DatePicker and TimePicker
Calendar, Clocks, DatePicker and TimePicker
 
Progress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialogProgress Dialog, AlertDialog, CustomDialog
Progress Dialog, AlertDialog, CustomDialog
 
AutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextViewAutocompleteTextView And MultiAutoCompleteTextView
AutocompleteTextView And MultiAutoCompleteTextView
 
Web view
Web viewWeb view
Web view
 
Parceable serializable
Parceable serializableParceable serializable
Parceable serializable
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
 
Android Installation Testing
Android Installation TestingAndroid Installation Testing
Android Installation Testing
 
Android Installation
Android Installation Android Installation
Android Installation
 

Recently uploaded

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 

Recently uploaded (20)

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 

Content Providers in Android

  • 2. ContentProvider • Sharing Data between Different Apps
  • 3. Content URIs • To Query, we have to specify the query string in the form of a URI which has following format − • <prefix>://<authority>/<data_type>/<id> • Example content://contacts/people/5 • This is used to get the 5th contact from contact list
  • 4. What you can do with ContentProvider
  • 5. How to Access Content Provider • / Queries the user dictionary and returns results • mCursor = getContentResolver().query( • CONTENT_URI, // The content URI to access table • mProjection, // The columns to return for each row • mSelectionClause // Selection criteria • mSelectionArgs, // Selection criteria • mSortOrder); // The sort order for the returned rows
  • 7. • // A "projection" defines the columns that will be  returned for each row •  String[] mProjection = •  { •      // ContactsContract data table data1 column  name •      ContactsContract.CommonDataKinds.Phone.N UMBER         •  }; •   Example
  • 9. • // Remember to insert code here to check  for invalid or malicious input. •    •  // Constructs a selection clause that  matches the contact_id from data table. •  mSelectionClause =  ContactsContract.CommonDataKinds.Phon e.CONTACT_ID + " = ?";
  • 10. • // Get data for 1st record •  mSelectionArgs[0] = 1; •  // Does a query against the table and returns a Cursor  object •  mCursor = getContentResolver().query(  •      ContactsContract.CommonDataKinds.Phone.CONTENT_ URI, // URI •      mProjection,                 // The columns to return for  each row •      mSelectionClause             // Data for first contact •      mSelectionArgs,              // first contact •      mSortOrder                   // The sort order for the  returned rows •            );                  •    •  
  • 11. • // Some providers return null if an error occurs, others throw an exception • if (null == mCursor) { • // Insert code here to handle the error. Be sure not to use the cursor! You may want to • // call android.util.Log.e() to log this error. • // If the Cursor is empty, the provider found no matches •
  • 12. • } else if (mCursor.getCount() < 1) { • // Insert code here to notify the user that the contact query was unsuccessful. This isn’t necessarily • // an error. You may want to offer the user the option to insert a new row, or re-type the • // search term. • } else { • • // Insert code here to do something with the results • • // Moves to the next row in the cursor. Before the first movement in the cursor, the • // "row pointer" is -1, and if you try to retrieve data at that position you will get an • // exception. • •
  • 13. • while (mCursor.moveToNext()) { • • // Gets the value from the column. • • phoneNumber = mCursor.getString(index); • • // Show phone number in Logcat • Log.i("Phone"," Numbers : " + phoneNumber); • • // end of while loop • } • • }
  • 14. PERMISSION • This will not work in android 6.0 and above • You have to explicitly request for permissions
  • 15. Check Whether contact read permission is there or not • // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { • //Not having permission • } • Else { • //having permission • }
  • 16. If Not Requestion Permission • ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CONTACTS} , MY_PERMISSIONS_REQUEST_READ_C ONTACTS);
  • 17. Handle permission • @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { • //Read Cursor Now • } • }