You might want to use contacts more or less when you're doing the app, and according to Google's API, you need to write a lot of code, such as querying the database first, involving database tables and fields and corresponding SQL statements.
So can we add a contact module to our project, this makes it easier to use mobile phone contacts, the answer is yes, and as a stand-alone module, so in your other app, you can introduce the module, or you can upload the module to the MAVEN library, easy to use.
So let's take a look at the effect first:
The first is the item of each module involved in the future.
Then the individual tests in a single module:
For example, what we need now is a built-in app for native apps, you need to click on a contact button, or you need a custom interface, and you click on the contact's custom UI button.
This is when you select a custom contact, and then select a contact with more than one phone, you need to choose:
The return result is:
is not very simple, then how do we achieve it:
Encapsulated code calls
Here, I show my code writing style, you need to add a click event for the button, such as the contact button, when clicking on the contact button:
ContactFactory.newContact(this).getContacts();
Call the static NewContact method in the factory class Contactfactory, and then call the Getcontacts method to implement the actions for the built-in contacts in the system's native app.
Everyone should know that when you are done in contact, such as selecting a contact, you need to do a second query on the currently selected ID, at which point we will receive the data to rewrite the Onactivityresult method.
And if you call the Startactivityforresult method in fragment, you need to rewrite the onactivityresult in your fragment class so that you can receive the data correctly.
So, after the encapsulation is complete, you just need to rewrite the Onactivityresult method of the class after you call Getcontacts, and then call in the class:
Contactfactory.newcontact (This). Onactivityresult (Requestcode, ResultCode, data, new Contactcallback () {
@ Override public
void onsuccess (@NonNull string contactnumber, @NonNull string contactname) {
Mtext.settext ( ContactName + contactnumber);
}
@Override public
void onfailed (@NonNull int errcode, @NonNull String message) {
Mtext.settext (errcode + message) ;
}
});
You need to have an anonymous inner class that will return Onsuccess and Onfailed methods, and then for contacts and phone numbers, you can do the relevant presentation.
Post-Encapsulation Summary
Call in one of your activity classes or fragment:
To override a method in this class:
ContactFactory.newContact(this).onActivityResult(requestCode, resultCode, data, null);
Summarize
I think the package for the contact is still very necessary, and then you can also see the relevant source through Https://github.com/neuyu/android-best-practices/tree/master/app, welcome to star and Fork, Later will be a separate module uploaded to the MAVEN library, convenient for everyone to use, but also hope that we can give the relevant module development to provide valuable advice, thank you.