android通过ContentProvider实现跨进程读写数据

平时开发过程中,会遇到这种现象,某些apk中的数据需要其它APK访问。处理这种需求的方法很多。下面主要介绍使用ContentProvider实现跨进程读写数据方法。


 private static final String KEY_MEM_BIG_APPS = "mem_big_apps";

 private ContentResolver mResolver;


读取:

final String str = Settings.Global.getString(mResolver,KEY_MEM_BIG_APPS);


写入:

String bigMemsAppsStr = TextUtils.join(",",tmpBigApps);
Settings.Global.putString(mResolver,KEY_MEM_BIG_APPS,bigMemsAppsStr);



示例代码:

  private void summary(){
        if (MEM_APP_RECORD == null || MEM_APP_RECORD.size()==0) return;
        Set tmpBigApps = null;
        AppMem appMem ;
        for (String packageName:MEM_APP_RECORD.keySet()){
            appMem = MEM_APP_RECORD.get(packageName);
            if (appMem.isOver()){
                if (tmpBigApps == null){
                    tmpBigApps = new HashSet<>(mBigMemApps);
                }
                tmpBigApps.add(packageName);
            }
        }
        if (tmpBigApps == null || tmpBigApps.size()==0) return;
        String bigMemsAppsStr = TextUtils.join(",",tmpBigApps);
        Settings.Global.putString(mResolver,KEY_MEM_BIG_APPS,bigMemsAppsStr);

        if (tmpBigApps != null && tmpBigApps.size()>0) {
            for (String p : tmpBigApps) {
                MEM_APP_RECORD.remove(p);
            }
        }
    }

你可能感兴趣的:(手机常规设置,读写,跨进程,数据共享)