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