Content provider (1)
Next, we will introduce the content resolver, one of the four components of Android
The content provider is the external access interface provided by Android internal program, which can share data.
Requirements: obtain the address book information through the content provider, and display it in your own program using ListView.
First, let's introduce the permissions we need to access the address book this time
<uses-permission android:name="android.permission.READ_CONTACTS"/> //Permissions required to access the address book
Let's put the activity main.xml file information first
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_item_01"> </ListView> </android.support.constraint.ConstraintLayout>
Next, let's put the java code in the main activity section
package com.example.jackson.contentresolver_01; import android.Manifest; import android.content.pm.PackageManager; import android.database.Cursor; import android.provider.ContactsContract; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ArrayAdapter<String> arrayAdapter; List<String> arrayList=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView=(ListView) findViewById(R.id.list_item_01); //Encapsulate the data into the collection and data binding through ArrayAdapter and ListView arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList); listView.setAdapter(arrayAdapter);//Binding succeeded in binding data in ArrayAdapter to ListView //Read? Contacts is the permission to apply //The following code is used to determine whether the user authorizes the required permissions if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) !=PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},1); //Three parameters of requestPermissions: MeinActivity instance. Use the array to pass in the required permissions and return the result code } else//If the authorization is successful, the Read() method is called to store the data in the ArrayList < > collection { Read(); } } //Use content providers provided by the system private void Read() { Cursor cursor=null; try { //Query program information and return value is still cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); if (cursor!=null) while (cursor.moveToNext()) { //Index data by key value String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); arrayList.add(name+"\n"+number); //Using ArrayLIst to store data } arrayAdapter.notifyDataSetChanged(); //Refresh the LIstView control information using the notifyDataSetChanged method } catch (Exception e) { e.printStackTrace(); } finally { if (cursor!=null) { cursor.close(); //Don't forget to turn off content providers } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: //Analyze the user authorization result by judging the request code in the grantResults [] array if (grantResults.length>0&&grantResults[0] ==PackageManager.PERMISSION_GRANTED) { Read(); //If the authorization is successful, the Read() method is called to store the data in the ArrayList < > collection } else { Toast.makeText(MainActivity.this,"Authorization failed",Toast.LENGTH_SHORT).show(); //Users will be prompted if the authorization is successful } } } }
Finally, don't forget to enter the required permissions in the Android manifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jackson.contentresolver_01"> <uses-permission android:name="android.permission.READ_CONTACTS"/> //Permissions required to access the address book <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's put the effect here. This is the information I added to the address book
OK, I will continue to introduce the knowledge of content providers later, such as the content providers that create their own programs.