AndroidManifest.xml文件注册
android:name=".provider.InfoDataProvider"
android:authorities="com.h.lo.provider"
android:exported="true"/>
import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.support.annotation.Nullable; import com.hybunion.ipos.greendao.bean.GreenDaoManager; import com.hybunion.ipos.greendao.bean.MerInfoBean; import com.hybunion.ipos.utils.Constants; /** * Created by hxs on 2016/8/16. */ public class InfoDataProvider extends ContentProvider { private final static String AUTHORITY = "com.h.lo.provider"; private static UriMatcher uriMatcher; private static final int GET_MID = 200;//mid private static final int GET_TID = 201;//tid private static final int SET_TID = 202;//设置TID private static final int SET_MID = 203;//设置MID private static final int SET_MERINFOBYMID = 204;//通过mid设置商户信息 private static final int GET_MERINFOBYMID = 205;//通过mid取得商户信息 static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY, "getTid", GET_TID); uriMatcher.addURI(AUTHORITY, "getMid", GET_MID); uriMatcher.addURI(AUTHORITY, "getMerInfoByMid", GET_MERINFOBYMID); uriMatcher.addURI(AUTHORITY, "setTid", SET_TID); uriMatcher.addURI(AUTHORITY, "setMid", SET_MID); uriMatcher.addURI(AUTHORITY, "setMerInfoByMid", SET_MERINFOBYMID); } @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int code = uriMatcher.match(uri); MerInfoBean merBeanquery = GreenDaoManager.getInstance().getMerInfoById(Constants.MERINFOINDEX); switch (code) { case GET_MID: String mid = merBeanquery.getMid(); String[] columns = new String[]{Constants.MID}; MatrixCursor cursor2 = new MatrixCursor(columns); cursor2.addRow(new String[]{mid}); return cursor2; case GET_TID: String tid = merBeanquery.getTid(); String[] columns3 = new String[]{Constants.TID}; MatrixCursor cursor3 = new MatrixCursor(columns3); cursor3.addRow(new String[]{tid}); return cursor3; case GET_MERINFOBYMID: String merinfobymid = merBeanquery.getMerInfo(); String[] columns4 = new String[]{Constants.MerInfoByMid}; MatrixCursor cursor4 = new MatrixCursor(columns4); cursor4.addRow(new String[]{merinfobymid}); return cursor4; default: throw new IllegalArgumentException("Unknow Uri :" + uri); } } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { int code = uriMatcher.match(uri); switch (code) { case SET_TID: String tid = values.getAsString(Constants.TID); merBeaninsert.setTid(tid); break; case SET_MID: String mid = values.getAsString(Constants.MID); merBeaninsert.setMid(mid); break; case SET_MERINFOBYMID: String merByMid = values.getAsString(Constants.MerInfoByMid); merBeaninsert.setMerInfo(merByMid); break; } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
set的地方的方法:
Uri uri = Uri.parse("content://com.h.lo.provider/setMid"); ContentValues values = new ContentValues(); values.put(Constants.MID, aId); getContentResolver().insert(uri, values);
Uri uri = Uri.parse("content://com.h.lo.provider/setTid"); ContentValues values = new ContentValues(); values.put(Constants.TID, aId); getContentResolver().insert(uri, values);
Uri uri = Uri.parse("content://com.h.lo.provider/setMerInfoByMid"); ContentValues values = new ContentValues(); values.put(Constants.MerInfoByMid, merInfoStr); getContentResolver().insert(uri, values);
使用时get地方的方法:
ContentResolver provider = this.getContentResolver(); Cursor cursorMid; try { //mid Uri uriMid = Uri.parse("content://com.h.lo.provider/getMid"); cursorMid = provider.query(uriMid, null, null, null, null); if (null != cursorMid) { cursorMid.moveToFirst(); mid = cursorMid.getString(0); LogUtil.e("yy+++----getMid=", mid); cursorMid.close(); } } catch (Exception e) { e.printStackTrace(); }
Cursor cursorTid; try { Uri uriTid = Uri.parse("content://com.h.lo.provider/getTid"); cursorTid = provider.query(uriTid, null, null, null, null); if (null != cursorTid) { cursorTid.moveToFirst(); tid = cursorTid.getString(0); LogUtil.e("yy+++----getTid=", tid); cursorTid.close(); } } catch (Exception e) { e.printStackTrace(); }
Cursor cursorMerInfo; try { Uri uriMerInfo = Uri.parse("content://com.h.lo.provider/getMerInfoByMid"); cursorMerInfo = provider.query(uriMerInfo , null, null, null, null); if (null != cursorMerInfo) { cursorMerInfo.moveToFirst(); merInfo= cursorMerInfo.getString(0); LogUtil.e("yy+++----getMerInfo=", merInfo); cursorMerInfo.close(); } } catch (Exception e) { e.printStackTrace(); }