不同进程访问”相同“资源

今天碰到一个服务在配置文件里 android:process=":updataService" 是个独立的线程

在Application里我有个SharedPreferences统一管理单例工具,

当我在Service里访问并设置了一个SP(发现会new这个SP工具!!!)

但在Application反问时发现这SP并没有值(只是默认值),

研究半天 猜想是跨进程服务导致我的SP访问不到,还是进程不同步呢?

后面我改成单例传进的是全局的Application ,不传Service 的mContext!

后记:

如是是通知栏里点击启动服务

//PendingIntent.FLAG_UPDATE_CURRENT更新当前Intent ,否则在onStartCommand无法获取数据

 PendingIntent.getService(mContext, 0, startDownAction(), PendingIntent.FLAG_UPDATE_CURRENT);

private Intent startDownAction() {
current_state = DOWNLOAD_START;
Intent intent = new Intent(mContext, UpdateService.class);
intent.putExtra("isNotify",true);
return intent;
}

原文地址:https://www.cnblogs.com/gfqFighting/p/3991167.html