Android operation contact android development tutorial, android contact

Source: Internet
Author: User

Android operation contact android development tutorial, android contact

Contacts in the Android system also provide external data through ContentProvider. Here we can obtain all contacts, obtain contacts through phone numbers, add contacts, and use transactions to add Contacts.

Get all contacts

1. Contacts in the Android system provide data externally through ContentProvider.

2. database path:/data/com. android. providers. contacts/database/contacts2.db

3. We need to pay attention to three tables.

Raw_contacts: the contact id is saved.

Data: it is a many-to-one relationship with raw_contacts. It stores various contact data.

Mimetypes: Data Type

4. The authorites of the Provider is com. android. contacts.

5. The path for querying the raw_contacts table is contacts.

6. The path for querying the data table is contacts/#/data

This path is for connection query. to query the "mimetype" field, you can query the data in the mimetypes table according to "mimetype_id ".

7. First query raw_contacts to get the id of each contact. Then, use the id to query the corresponding data from the data table and classify the data according to mimetype.

Example:

 

// Query all contacts

Public void testGetAll (){

ContentResolver resolver = getContext (). getContentResolver ();

Uri uri = Uri. parse ("content: // com. android. contacts/contacts ");

Cursor idCursor = resolver. query (uri, new String [] {"_ id"}, null );

While (idCursor. moveToNext ()){

// Obtain the id in the raw_contacts table

Int id = idCursor. getInt (0 );

// Query the data in the data table based on the obtained ID

Uri = Uri. parse ("content: // com. android. contacts/" + id + "/data ");

Cursor dataCursor = resolver. query (uri, new String [] {"data1", "mimetype"}, null );

StringBuilder sb = new StringBuilder ();

Sb. append ("id =" + id );

// Query

While (dataCursor. moveToNext ()){

String data = dataCursor. getString (0 );

String type = dataCursor. getString (1 );

If ("vnd. android. cursor. item/name". equals (type ))

Sb. append (", name =" + data );

Else if ("vnd. android. cursor. item/phone_v2". equals (type ))

Sb. append (", phone =" + data );

Else if ("vnd. android. cursor. item/email_v2". equals (type ))

Sb. append (", email =" + data );

}

System. out. println (sb );

}

}

 

Obtain a contact by phone number

1. The system provides the ability to obtain data in the data table based on the phone number. The path is data/phones/filter /*

2. Replace "*" with the phone number to find the required data. Get "display_name" to get the display name of the contact.

Example:

// Query the contact name by phone number

Public void testGetName (){

ContentResolver resolver = getContext (). getContentResolver ();

Uri uri = Uri. parse ("content: // com. android. contacts/data/phones/filter/1111 ");

Cursor c = resolver. query (uri, new String [] {"display_name"}, null );

While (c. moveToNext ()){

System. out. println (c. getString (0 ));

}

}

Add contact

1. Insert the id to the raw_contacts table first. The path is raw_contacts.

2. Get the id and insert the data to the data table. The path is data.

Example:

// Add a contact

Ublic void testInsert (){

ContentResolver resolver = getContext (). getContentResolver ();

Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts ");

ContentValues values = new ContentValues ();

// Insert a record other than the ID to raw_contacts, all of which are NULL. The ID is automatically generated.

Long id = ContentUris. parseId (resolver. insert (uri, values ));

// Add a contact name

Uri = Uri. parse ("content: // com. android. contacts/data ");

Values. put ("raw_contact_id", id );

Values. put ("data2", "FHM ");

Values. put ("mimetype", "vnd. android. cursor. item/name ");

Resolver. insert (uri, values );

// Add a contact number

Values. clear (); // clear the previous data

Values. put ("raw_contact_id", id );

Values. put ("data1", "18600000000 ");

Values. put ("data2", "2 ");

Values. put ("mimetype", "vnd. android. cursor. item/phone_v2 ");

Resolver. insert (uri, values );

// Add a contact email

Values. clear ();

Values. put ("raw_contact_id", id );

Values. put ("data1", "zxx@itcast.cn ");

Values. put ("data2", "1 ");

Values. put ("mimetype", "vnd. android. cursor. item/email_v2 ");

Resolver. insert (uri, values );

 

Use transactions to add Contacts

1. the Provider is accessed multiple times when a contact is added. If an exception occurs during the process, data is incomplete. These operations should be placed in a transaction.

2. Use the ContentResolver's applyBatch (String authority, ArrayList <ContentProviderOperation> operations) method to execute multiple operations in one transaction.

Example:

// Use transactions to add Contacts

Public void testInsertBatch () throws Exception {

ContentResolver resolver = getContext (). getContentResolver ();

 

ArrayList <ContentProviderOperation> operations = new ArrayList <ContentProviderOperation> ();

 

ContentProviderOperation operation1 = ContentProviderOperation //

. NewInsert (Uri. parse ("content: // com. android. contacts/raw_contacts "))//

. WithValue ("_ id", null )//

. Build ();

Operations. add (operation1 );

 

ContentProviderOperation operation2 = ContentProviderOperation //

. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//

. WithValueBackReference ("raw_contact_id", 0 )//

. WithValue ("data2", "ZZH ")//

. WithValue ("mimetype", "vnd. android. cursor. item/name ")//

. Build ();

Operations. add (operation2 );

ContentProviderOperation operation3 = ContentProviderOperation //

. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//

. WithValueBackReference ("raw_contact_id", 0 )//

. WithValue ("data1", "18612312312 ")//

. WithValue ("data2", "2 ")//

. WithValue ("mimetype", "vnd. android. cursor. item/phone_v2 ")//

. Build ();

Operations. add (operation3 );

 

ContentProviderOperation operation4 = ContentProviderOperation //

. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//

. WithValueBackReference ("raw_contact_id", 0 )//

. WithValue ("data1", "www.2cto.com ")//

. WithValue ("data2", "2 ")//

. WithValue ("mimetype", "vnd. android. cursor. item/email_v2 ")//

. Build ();

Operations. add (operation4 );

 

// Batch Execute multiple operations in the transaction

Resolver. applyBatch ("com. android. contacts", operations );

}

 


Android tutorial

1. Quickly view the mobile phone charging status

Method 1: view through the standby screen. As we all know, the Android system has designed the automatic screen lock function. When the mobile phone is not used for a period of time, the background light is automatically disabled, if you need to activate it, press the Menu key of your phone twice. However, after you press it for the first time, the Standby interface is displayed, showing the current battery charging status of your phone.

Method 2: In the "Status" option of the "AboutPhone" option in the Android system, the "BatteryLevel" option is displayed. This shows the charging Status of the current mobile phone.

Method 3: check through third-party software. Because Android does not have such software, if you want to view the current charging status of your mobile phone in detail, use a third-party battery to check the software.

2. How to Set invisible passwords

If you have used some applications to log on to, you may know that the entered password is displayed by default when you enter the account password. For the sake of security, you do not want to see your own password, so how can I set an invisible password for the Android phone? How to set the password not to be displayed.

The password Settings are in SecurityLocation. Therefore, first press the Menu key on the main screen and select the last "Settings" from the advanced Menu ";

Go to the settings menu and slide down the scroll bar. Find the "SecurityLocation" category and click to enter;

Find the Passwords category in the security and location classification menu, and remove the check box next to the "Visiblepasswords" option.

3. Solution to Market download failure

In fact, this problem does not exist in foreign countries. It is only possible for domestic friends to encounter this problem, because it seems that Google servers are blocked in China, therefore, if we need a service such as YOUTUBE, we can only "flip the wall.

The static IP Address Setting method can be used to solve the problem for all users. This method is unique to Android123 and works with Google's latest services. After dozens of Android phones, the app can be downloaded normally.

First, select "Settings -- wirelesscontrals -- WIFISettings -- Menu -- Adbanced", select USEStaticIP, and set DNS1 to 8.8.8.8 and DNS2 to 8.8.4.4.

That's right. The method mentioned above uses Google's latest DNS service. After filling in the content, you do not need to set it.

Iv. Alternative methods for setting Droid ringtones

When setting ringtones for other Android phones, we generally use third-party software (such as TongPicker) to set ringtones. This setting method is cumbersome, you must start the program in the corresponding setting box to set it. This time, Android123 introduces you to the method of using the built-in ringtone library.

In simple terms, this method is to set the corresponding folder in the built-in TF card. The folder must be set according to the specified name, otherwise the system will not recognize it. We can use this method to set the sound of incoming call ringtones, text message ringtones, alarms, and system prompts.

First, set a folder named media in the root directory of the TF card, set the audio folder in the folder, and then set other corresponding folders in the audio folder.

The name of the folder where the SMS ringtone is placed is notifications, the folder where the ringtone is placed is alarms, the folder where the ringtone is placed is ringtones, And the folder where the system tone is named ui.

After setting the folder, place the corresponding ringtone file in the corresponding folder. In this way, we can directly see and select these ringtone files in the settings of the Android system.

5. The ringtones in the Android storage card folder are not displayed.

There are many ways to set ringtones for Android phones, but... the rest is full>

New Android Tutorials: new Android development tutorials. Where can I help you?

Android: HTC G1 operating interface Android is an open-source mobile phone operating system developed by Google based on the Linux platform. It includes the operating system, user interfaces, and applications-all software required for mobile phone work, and there are no exclusive obstacles that have previously hindered innovation in the mobile industry. Google has developed Android in partnership with the Open Mobile Alliance, which is a leader in more than 30 technologies and wireless applications including China Mobile, Motorola, Qualcomm, HTC, and T-Mobile. Through in-depth partnerships with carriers, equipment manufacturers, developers, and other parties, Google hopes to build a standardized and open mobile phone software platform, an open ecosystem is formed in the mobile industry. Take a look at the following android tutorial, which is very comprehensive and detailed: tutorial 1: Android development video training tutorial android smart System Development tutorial size: 3.5G tutorial 2: google Android development beginner and practical video tutorial + Source Code e-book size: 1 GB tutorial 3: Android development from scratch video tutorial size: 1.19 GB tutorial 4: android 2.0 game development practice example tutorial + source code size: 1.8 GB tutorial 5: Android-based Address Book Development tutorial video size: 437 MB tutorial 6: 3G Mobile Phone development Android Application Development size: 4.23 GB tutorial 7: Android project video tutorial + Source Code tutorial size: 1.85 GB tutorial 8: android from entry to proficient tutorial video tutorial size: 117 MB tutorial 9: android Application Development details (ebook) Format: PDF tutorial 10: How to Increase Android size: 2.7 GB tutorial 11: 3G application development size of Android practical courses: 1.7 GB tutorial address: 139url.cn/kv875f

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.