App Components - Orange168/NotesOnReading GitHub Wiki

[TOC] ####Content URIs content://user_dictionary/words
content://user_dictionary ==database, words == table Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);

[[1]ContactsContract][ContactsContract.RawContacts]

  • Insert
 ContentValues values = new ContentValues();
 values.put(RawContacts.ACCOUNT_TYPE, accountType);
 Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
 long rawContactId = ContentUris.parseId(rawContactUri);
 
  values.clear();
 values.put(Data.RAW_CONTACT_ID, rawContactId);
 getContentResolver().insert(Data.CONTENT_URI, values);

The batch method is by far preferred.

ArrayList<ContentProviderOperation> ops =
         new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
         .withValue(RawContacts.ACCOUNT_TYPE, accountType)
         .build());

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         // withValueBackReference  unknown index
         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
         .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
         .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
         .build());

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
  • Update
  • Delete
  • Query
 Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
          new String[]{RawContacts._ID},
          RawContacts.CONTACT_ID + "=?",
          new String[]{String.valueOf(contactId)}, null);
//reuse the URI:
Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon()
          .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
          .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)
          .build();
 Cursor c1 = getContentResolver().query(rawContactUri,
          RawContacts.STARRED + "<>0", null, null, null);
 ...
 Cursor c2 = getContentResolver().query(rawContactUri,
          RawContacts.DELETED + "<>0", null, null, null);
 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
Cursor c = getContentResolver().query(entityUri,
         new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
         null, null, null);
try {
    while (c.moveToNext()) {
        String sourceId = c.getString(0);
        if (!c.isNull(1)) {
            String mimeType = c.getString(2);
            String data = c.getString(3);
            ...
        }
    }
} finally {  c.close();  }
int index = mCursor.getColumnIndex(UserDictionary.Words.WORD);
while (mCursor.moveToNext()) {
       // Gets the value from the column.
       newWord = mCursor.getString(index);
       // Insert code here to process the retrieved word.
       // end of while loop
   }

###Content Provider Permissions

  1. opern all provider permission
<permission android:name="orange.permission.READ_CONTENTPROVIDER" 
    android:label="Allow read content provider" 
    android:protectionLevel="normal" />  
    …… 
<provider android:name=".PrivProvider" 
    android:authorities="com.orange.provider.NotePad" 
    android:readPermission="orange.permission.READ_CONTENTPROVIDER"
    android:exported="true" > 
</provider> 

other application read this provider need add <uses-permission android:name="orange.permission.READ_CONTENTPROVIDER"/> 2. path permission

<provider android:name=".PrivProvider" 
    android:authorities="com.orange.provider.NotePad" 
    android:readPermission="orange.permission.READ_CONTENTPROVIDER"
	android:exported="true" >
<path-permission android:pathPrefix="/hello" android:readPermission="READ_HELLO_CONTENTPROVIDER" />
</provider> 
  1. grant App1(has App3 provider permission) create a intent to App2(no permission), App2 can read App3 provider
android:grantUriPermissions="true" 
Intent intent = new Intent(this,ReadProvider.class); 
intent.setClassName("com.example.propermissiongrant", "com.example.propermissiongrant.MainActivity");
intent.setData(Uri.parse("content://com.orange.permission.propermission.PrivProvider/world/1"));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  //传递权限 
startActivity(intent); 

4.path grant permission <grant-uri-permission android:pathPrefix="/hello" /> ###MIME Type Reference like text/html provider text vnd.android.cursor.dir multi row vnd.android.cursor.item for a single row

⚠️ **GitHub.com Fallback** ⚠️