跨进程:Client进程只不过是持有了Server端的代理;代理对象协助驱动完成了跨进程通信。
对于一些需求,比如切换语言重启应用,如果项目中很多地方有通过Application.getResource().getString(int stringRes)
在部分手机可能存在语言没有切换的问题,比较简单的能解决这个问题的方式是,杀死当前进程,重新启动app。
具体实现:
1、子进程服务
/**
* 跨进程重启,避免用Application获取的文案,切换语言后文案未更新问题
*/
public class RestartService extends Service {
public static final int MSG_CONNECTED = 1;
public static final int MSG_KILL_APP = 2;
public static final int MSG_RESTART_APP = 3;
private String mPackageName;//要重启的app的包名
@Override
public IBinder onBind(Intent intent) {
if (intent != null) {
mPackageName = intent.getStringExtra(AppRestartControl.PACKAGE_BUNDLE_KEY);
}
Messenger restartMessenger = new Messenger(new ServerMsgHandler(this, mPackageName));
return restartMessenger.getBinder();
}
private static class ServerMsgHandler extends Handler {
private Context mContext;
private String mPackageName;
public ServerMsgHandler(Context context, String packageName) {
mContext = context;
mPackageName = packageName;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CONNECTED://收到app进程发来的消息,binder连接成功
try {
msg.re