Android Four components Learning ContentProvider three

Source: Internet
Author: User

Last lesson Learn how to create a contentprovider yourself and use Contentresolver to manipulate contentprovider.

Today we use the ContentProvider provided by the system. First, a simple, read the system's SMS content.

As you know from the previous section, if you want to read the system's text messages, you have to find the system's SMS there?

First find the text message database problem, and then export, to see what's inside

Now that we've analyzed it, what we need and what we don't need. Of course you also need to know the URL of the SMS Provider to the third party. This can be viewed through the Android source code.

Then we'll write a program that reads the SMS from the system.

First look at the code in the activity:

public class Readmessageactivity extends Activity {Button readmessage; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_readmessage) Readmessage = (Button) Findviewbyid (R.id.button1); Readmessage.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View arg0) {log.i ("readmessageactivity", "Read contact button pressed"); Contentresolver cresolver = Getcontentresolver ();/** * URI: is the URL of the SMS Provider, which can be obtained by viewing the source code * projection: Several fields that need to be queried, take care of that query on the line * * cursor cursor = cresolver.query (Uri.parse ("Content://sms"), New string[]{"Address", "date", "type", "body"}, NULL, NULL, NULL); while (Cursor.movetonext ()) {String address = cursor.getstring (Cursor.getcolumnindex ("Address")); String date = cursor.getstring (Cursor.getcolumnindex ("date")); String type = cursor.getstring (Cursor.getcolumnindex ("type")); String BODY = cursor.getstring (Cursor.getcolumnindex ("body")); LOG.I ("readmessageactivity", Address + ";"+ Date +"; "+ type +"; "+ Body);}});}} 

The layout file is a button, not explained in detail.

Printing results are:

Ok. Reading the system text message is simply done. Then we write a text message to the system.

Let's add a button that inserts a text message and then insert a text message when you press it:

The code is as follows:

Writemessage.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {// The text message button was pressed contentresolver cresolver = Getcontentresolver (); Contentvalues values = new Contentvalues (); Values.put ("Body", "Sorry, you because tail number is 6789 mobile phone number, the cost of 1w yuan, has been shut down!" Values.put ("Address", 10086), Values.put ("date", System.currenttimemillis ()), Values.put ("type", 1); Cresolver.insert (Uri.parse ("content://sms"), values);});

The results are as follows:

You can see the information that has been inserted into the database, and the dialog box appears. So inserting a text message is done.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Four components learning ContentProvider three

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.