-
子进程创建的时候,会从父进程继承一份全局变量,全局变量的值都是初始值。
备注:不管是在修改全局变量之前还是之后创建的子进程,父进程对这些变量的修改都是不会同步到子进程中去的。 -
子进程和父进程共享assets目录下的文件。
-
子进程和父进程共享data/data/packagename/目录下的文件。
备注:子进程和父进程共享私有目录下的文件,各自修改该目录下的文件都会影响到另一个进程。
可以通过下面的例子验证上述结论:
首先新建一个Android工程,并在工程的assets目录下放一个文件"test.txt",文件的内容就是简单的几个字符,如下:
“test.test”
Manifest.xml中声明两个activity,其中SecondActivity声明在另一个进程中运行:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:process=":secondprogress">
</activity>
MainActivty的代码如下:
public class MainActivity extends AppCompatActivity {
public static int TEXT1_INT = 100;
public int TEXT2_INT = 200;
public static String CONST1 = "aaa";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TEXT1_INT = 101;
TEXT2_INT = 201;
CONST1 = "bbb";
FileUitls.copyAssetsToFile(MainActivity.this,"test.txt","copy.txt");
}
private void initView() {
TextView tv1 = (TextView) findViewById(R.id.tv_tv1);
tv1.setText("assets文件里面的内容:"+FileUitls.getAssetFile(MainActivity.this,"test.txt"));
tv1.append("\n");
tv1.append("MainActivity.TEXT1_INT:"+ TEXT1_INT);
tv1.append("\n");
tv1.append("MainActivity.TEXT2_INT:"+ TEXT2_INT);
tv1.append("\n");
tv1.append("MainActivity.CONST1:"+ CONST1);
tv1.append("\n");
tv1.append("files目录下文件里面的内容:"+FileUitls.getFile(MainActivity.this,"copy.txt"));
}
@Override
protected void onResume() {
super.onResume();
initView();
FileUitls.copyAssetsToFile(MainActivity.this,"test.txt","copy.txt");
}
public void goToSecondPage(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
SeondActivity的代码如下:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
}
private void initView() {
TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setText("assets文件里面的内容:"+FileUitls.getAssetFile(SecondActivity.this,"test.txt"));
tv2.append("\n");
tv2.append("MainActivity.TEXT1_INT:"+MainActivity.TEXT1_INT);
tv2.append("\n");
tv2.append("MainActivity.TEXT2_INT:"+ new MainActivity().TEXT2_INT);
tv2.append("\n");
tv2.append("MainActivity.CONST1:"+ MainActivity.CONST1);
tv2.append("\n");
tv2.append("files目录下文件里面的内容:"+FileUitls.getFile(SecondActivity.this,"copy.txt"));
FileUitls.copyAssetsToFile(SecondActivity.this,"test.txt","copy.txt");
}
}
MainActivity和SecondActivity在显示相应的内容后都会调用FileUitls.copyAssetsToFile()方法修改files目录下文件的内容。
FileUitls.copyAssetsToFile方法就是将assets中test.txt文本中的内容复制到files目录下,同时将当前context的内存地址的值也写入文件中,其代码如下:
public static void copyAssetsToFile(Context context, String fromAssetsFileName,String toFileName){
String content = "";
InputStream is = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
Writer writer = null;
AssetManager assetManager = context.getResources().getAssets();
try {
is = assetManager.open(fromAssetsFileName);
inputStreamReader = new InputStreamReader(is);
bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
char[] charArray = new char[1024];
int readLength = -1;
while((readLength = bufferedReader.read(charArray,0,charArray.length))!=-1){
stringBuilder.append(charArray,0,readLength);
}
content = stringBuilder.toString();
File dir = context.getFilesDir();
File file = new File(dir.getAbsolutePath()+File.separator+toFileName);
if(!file.exists()){
file.createNewFile();
}
writer = new FileWriter(file);
writer.write("copy的内容 "+context.toString()+":");
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
closeStream(is);
closeReader(bufferedReader);
closeReader(inputStreamReader);
closeWriter(writer);
}
}
运行工程,第一次显示第一个页面:
点击按钮打开第二个页面:
点击手机的Back键返回到第一个页面:
再次点击按钮打开第二个页面:
文章开头的结论为自己通过代码验证所得,若有错误请指正~