下面是helper文件 public class Maphelper { public ContentResolver cr; public String URI = "content://com.xpoesd.diy.apphide/person/"; public Maphelper(int ContextClass){ Context context; if (ContextClass==1){ context = (Context) callMethod(callStaticMethod(findClass("android.app.ActivityThread", null), "currentActivityThread", new Object[0]), "getSystemContext", new Object[0]); }else { context = MainActivity.getInstance(); } cr = context.getContentResolver(); } private void saveFile_C(String Filename,String FileValus) throws FileNotFoundException { OutputStream os = cr.openOutputStream(Uri.parse( URI+Filename)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); try { bw.write(""); bw.flush(); bw.write(FileValus); bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveFile(String Filename,String FileValus){ try { saveFile_C(Filename,FileValus); } catch (FileNotFoundException e) { e.printStackTrace(); } } private String getFile_C(String Filename) throws FileNotFoundException { InputStream is = cr.openInputStream(Uri.parse(URI+Filename)); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result=""; String result_=""; try { while((result = br.readLine() )!=null){ result_=result_+result; } //result = br.readLine(); br.close(); } catch (IOException e) { e.printStackTrace(); } return result_; } public String getFile(String Filename) { String str = ""; try { str = getFile_C(Filename); } catch (FileNotFoundException e) { e.printStackTrace(); } return str; } public void putString(String name,String values) { ContentValues value = new ContentValues(); value.put(name, values); Uri insertColumn = cr.insert(Uri.parse(URI), value); } public String getstring(String name) { Cursor cursorResultAll =cr.query(Uri.parse(URI),null,name, null, null); assert cursorResultAll != null; if (cursorResultAll.getCount()>0) { cursorResultAll.moveToNext(); String str = cursorResultAll.getString(1); //String str = cursorResultAll.getString(1); //int age = cursorResultAll.getInt(2); return str; } //MatrixCursor cursor =(MatrixCursor) cr.query(Uri.parse(URI),null,name, null, null); //cursor.getInt(2) 取数据类型 return ""; } }
ContentProvider文件
public class MapProvider extends ContentProvider { private static final String AUTHORITY = "com.xpoesd.diy.apphide"; private static final int TYPE_ALL = 1; private static final int TYPE_SINGLE = 2; public static final String CURSOR_COLUMN_NAME = "cursor_name"; public static final String CURSOR_COLUMN_VALUE = "cursor_value"; public static final String CURSOR_COLUMN_TYPE = "cursor_type"; private static Map maps; private static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { mUriMatcher.addURI(AUTHORITY, "person", TYPE_ALL); mUriMatcher.addURI(AUTHORITY, "person/#", TYPE_SINGLE); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { if (uri.toString().contains("content://"+AUTHORITY+"/person/")){ if (selection==null){ for(int i=0;i<selectionArgs.length;){ if (maps.containsKey(selectionArgs[i])){ maps.remove(selectionArgs[i]); } i++; } }else { if (maps.containsKey(selection)){ maps.remove(selection); } } return 1; } return -1; } @Override public String getType(Uri uri) { switch (mUriMatcher.match(uri)) { case TYPE_ALL: return "vnd.android.cursor.dir/person"; case TYPE_SINGLE: return "vnd.android.cursor.item/person"; default: return null; } } @Override public Uri insert(Uri uri, ContentValues values) { if (uri.toString().contains("content://"+AUTHORITY+"/person/")){ Set set = values.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { Object key = it.next();// it.next()返回的是set集合元素,也就是map中的键 maps.put(key,values.get(key.toString())); } } return uri; } @Override public boolean onCreate() { maps= JSON.parseObject("{}"); return true; } @Override public Cursor query(Uri uri, String[] columns, String selection, String[] selectionArgs, String orderBy) { // String[] path= uri.toString().split("/"); MatrixCursor cursor = new MatrixCursor(new String[]{CURSOR_COLUMN_NAME,CURSOR_COLUMN_VALUE,CURSOR_COLUMN_TYPE}); // if (path.length<1){return cursor;} // String key=path[path.length-1]; if (uri.toString().contains("content://"+AUTHORITY+"/person/")){ if (maps.containsKey(selection)){ Object[] rows=new Object[3]; rows[0]=selection; rows[1]=maps.get(selection); if (rows[1] instanceof Boolean) { rows[2]=1; }else if (rows[1] instanceof String) { rows[2]=2; }else if (rows[1] instanceof Integer) { rows[2]=3; }else if (rows[1] instanceof Long) { rows[2]=4; }else if (rows[1] instanceof Float) { rows[2]=5; } cursor.addRow(rows); } } return cursor; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (uri.toString().contains("content://"+AUTHORITY+"/person/")){ Set set = values.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { Object key = it.next();// it.next()返回的是set集合元素,也就是map中的键 maps.put(key,values.get(key.toString())); } } return 1; } @Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { File root = new File(getContext().getFilesDir() + "/" + uri.getPath()); root.getParentFile().mkdirs(); int imode = 0; if (mode.contains("w")) { imode |= ParcelFileDescriptor.MODE_WRITE_ONLY; if (!root.exists()) { try { root.createNewFile(); } catch (Exception ex) { } } } if (mode.contains("r")) imode |= ParcelFileDescriptor.MODE_READ_ONLY; if (mode.contains("+")) imode |= ParcelFileDescriptor.MODE_APPEND; return ParcelFileDescriptor.open(root, imode); } }
代码东拼西凑的,用网上找的模板修改的,csdn留存方便记忆,如果有抄袭部分请见谅。
使用方法
Maphelper maphelper = new Maphelper(0); String Bytes = JSON.toJSONString(maps); maphelper.putString(filename_xml, Bytes);
maphelper.getstring(name);