Application parsing Content Provider

Source: Internet
Author: User

Content Provider
Every app in Android runs in its own process, and when an app needs to return data from other apps, which requires data to be passed between different virtual machines, Google introduces content Provider. To address data sharing between different application packages

    • In Android, content provider is a special type of storage data that provides a standard set of interfaces for acquiring and manipulating data, and Android itself provides Contentprovider:contacts,brower, Calllog,mediastore. The app can use a specific content Provider with a unique content Provider interface, and then call the Contentresolver-provided method to use the content Provider you need.
    • ' Package Com.example.mybutton;

Import Android.content.ContentResolver;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.media.Image;
Import Android.provider.ContactsContract;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.GridView;
Import Android.widget.ImageView;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import java.util.List;

public class Mainactivity extends Appcompatactivity {
Private TextView Mtextview;
Private String text= "";

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mTextView= (TextView) findViewById(R.id.text_view);    ContentResolver cr=getContentResolver();    Cursor cursor=cr.query(ContactsContract.Contacts.CONTENT_URI,            null, null, null, null);    int nameIndex=0;    String ContactName="";    String contactId="";    Cursor phone;    String phoneNumber="";    int phoneIndex;    while(cursor.moveToNext())    {        //取得名称所在的索引        nameIndex=cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);        //通过所在索引取得联系人的名称        ContactName=cursor.getString(nameIndex);        //取得电话号码所在索引

Numberindex=cursor.getcolumnindex (ContactsContract.PhoneLookup.NUMBER);
String number=cursor.getstring (Numberindex);

        //获取联系人的ID号,在SQLite中的数据库ID        contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));        phone=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,                null, null);        while (phone.moveToNext()) {            phoneIndex=phone.getColumnIndex(ContactsContract                    .CommonDataKinds.Phone.NUMBER );            phoneNumber=phone.getString(phoneIndex);        }       text +=(ContactName+":"+phoneNumber+"\n");    }    cursor.close();    mTextView.setText(text);}

}

`

Application parsing Content Provider

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.