第一步:定义类继承 ContentProvider,实现相关接口
public class MyContentProvider extends ContentProvider
第二步:定义 UriMatcher
private final static String AUTHORITY="com.myprovider";//配置文件需要用到这个 private final static int INSERT_CODE=1; private final static int UPDATE_CODE=2; private final static int DELETE_CODE=3; private final static int SELECT_CODE=4; private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { Log.e(TAG,"-----uriMatcher--------"); uriMatcher.addURI(AUTHORITY, "insert", INSERT_CODE); uriMatcher.addURI(AUTHORITY, "update", UPDATE_CODE); uriMatcher.addURI(AUTHORITY, "delete", DELETE_CODE); uriMatcher.addURI(AUTHORITY, "selete", SELECT_CODE); }
第三步:匹配 增、删、改、查 URL
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.e(TAG,"query"); if(uriMatcher.match(uri)==SELECT_CODE){ Log.e(TAG,"query 匹配"); }else{ Log.e(TAG,"query fail"); } return null; }
第四步:配置文件
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> <provider android:exported="true" android:authorities="com.myprovider" android:name=".MyContentProvider"/> </application>
源码:
public class MyContentProvider extends ContentProvider { static String TAG="MyContentProvider"; private final static String AUTHORITY="com.myprovider"; private final static int INSERT_CODE=1; private final static int UPDATE_CODE=2; private final static int DELETE_CODE=3; private final static int SELECT_CODE=4; private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { Log.e(TAG,"-----uriMatcher--------"); uriMatcher.addURI(AUTHORITY, "insert", INSERT_CODE); uriMatcher.addURI(AUTHORITY, "update", UPDATE_CODE); uriMatcher.addURI(AUTHORITY, "delete", DELETE_CODE); uriMatcher.addURI(AUTHORITY, "selete", SELECT_CODE); } @Override public boolean onCreate() { return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.e(TAG,"query"); if(uriMatcher.match(uri)==SELECT_CODE){ Log.e(TAG,"query 匹配"); }else{ Log.e(TAG,"query fail"); } return null; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { Log.e(TAG,"insert"); if(uriMatcher.match(uri)==INSERT_CODE){ Log.e(TAG,"insert 匹配"); }else{ Log.e(TAG,"insert fail"); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.e(TAG,"delete"); if(uriMatcher.match(uri)==DELETE_CODE){ Log.e(TAG,"delete 匹配"); }else{ Log.e(TAG,"delete fail"); } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.e(TAG,"update"); if(uriMatcher.match(uri)==UPDATE_CODE){ Log.e(TAG,"update 匹配"); }else{ Log.e(TAG,"update fail"); } return 0; } }
第三方APP调用 Content Provider
Uri url = Uri.parse("content://com.myprovider/insert"); getContentResolver().delete(url,"ss",null);
原文地址:https://www.cnblogs.com/jtzp007/p/11494565.html
时间: 2024-11-07 19:59:41