Android uses intent to get contact information

Use of intent

After work, I looked at intent and found that I had forgotten a lot of knowledge. Then I reviewed it again and took notes here. The following are my summary points:

Explain why there are three components. We all know that the four components are activity, service, broadcastreceiver and ContentProvider. Intent is the bridge between the three components: activity, service and broadcastreceiver, and ContentProvider communicates through contentresolver, so it is the bridge between the three components.

When intent is used, it needs to define an action and data. Action is the action to be executed, and data is the data of the action to be executed.

For example, we have to make a call

Of course, don't forget to add permissions

View web page

Send an email with attachments

You can view maps and so on, but don't forget to add permissions. I forgot to say that although the Android system will ensure that every determined intent will be received by the built-in app of the system, we should verify whether an app accepts an intent before triggering it. (if an intent is triggered and no app will receive the intent, the app will crash.)

In order to verify whether an appropriate activity will respond to the intent, you need to execute queryintensities () to obtain a list of all activities that can receive the intent. If the returned list is not empty, then we can safely use this intent. For example:

Let's talk about some practical ones. We often encounter reading mobile phone contact information in development. Let's divide it into two parts and talk about reading contacts

Read individual contacts

Note that because the contact content is sensitive, those above 6.0 should be authorized dynamically, and the permission to read mobile phone contacts should be added from the manifest file.

Read a single contact through startactivityforresult and return a contact information you need. Of course, if you need all contact information, please don't worry. It will also be introduced below.

After pulling up the contact list through startactivityforresult, we need to restart the onactivityresult method and the result code specified by the second activity. Result if the operation is successful_ OK, if the user does not succeed in the operation, but directly clicks back or for any other reason, it is result_ Canceled, which is also result code. The default is result_ The reason for canceled is that there will be the data we need.

Read all contacts

The above is to obtain all contact data. If you need the detailed data of a single contact, you can modify it according to the parameters of obtaining all contacts.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>