Four components of Android content resolver

Keywords: Android github xml

ContentResolver

For complete code see: longlong's github

Standard format for content URI s:

  content://com.example.app.provider.table1
  content://com.example.app.provider.table2
The content URI is mainly composed of two parts: authority and path. The authority is generally named by package name. Path: distinguish different tables and put them behind the authority. Content: Protocol declaration. - > clearly indicate which table we want to access.
After getting the string of the content URI, it can be passed in as a parameter after being parsed into a URI object. (call URI. Parse() method).

Uri uri = Uri.parse("content://com.example.app.provider/table1")

Query data in table

Cursor cursir = getContentResolver().query(
    uri,
        projection,
        selection,
        selectionArgs,
        sortOrder);

Parameters:




After the query, extract the data one by one from the Cursor object: traverse all the rows of Cursor by moving the Cursor position, and then extract the data of each row

if(cursor != null){
            while(cursor.moveToNext()){
                String column1 = cursor.getString(cursor.getColumnIndex("column1"));
                String column2 = cursor.getString(cursor.getColumnIndex("volumn2"));
            }
            cursor.close();
        }

Add data

 ContextValues values = new ContextValues():
        values.put("column1","text");
        values.put("column2","1");
        getContentResolver(),insert(uri,values);

Update newly added data

ContextValues values = new ContextValues():
        values.put("column1", "");
        getContentResolver().update(uri, values, "column1 = ? column2 = ?", new String[]{"text", "1"});

The selection and selectionargs parameters are used to constrain the data to be updated to prevent all rows from being affected.

delete

getContentResolver().delete(uri,"column2 = ?",new String[]{"1"});

Read system contacts and realize dialing function

There is only one ListView in the layout LinearLayout.
In MainActivity:

public class MainActivity extends AppCompatActivity {
    ListView contactsview; //Declare a ListView object
    List<String> contactslist = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contactsview = (ListView) findViewById(R.id.contacts_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactslist);
        contactsview.setAdapter(adapter);
        readContacts();

        contactsview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Get the phone number to dial
                String phoneNum =contactslist.get(position);
                System.out.println(phoneNum);
                //Use Intent to switch to the calling interface, and send in the call to be broadcast,
                Intent in = new Intent();
                //Set the function to switch now
                in.setAction(Intent.ACTION_CALL);
                in.setData(Uri.parse("tel:" + phoneNum));
                startActivity(in);
            }
        });
    }
    private void readContacts() {
        Cursor cursor = null;

        cursor  = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {      //Move the cursor down and cycle: all lists are displayed. Traverse the cursor object
            String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactslist.add(displayName + "\n" + number);
        }
        cursor.close();
        //Turn off the Cursor object.
    }
}

Set permissions in the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

Posted by nainil on Wed, 01 Apr 2020 01:40:07 -0700