1主apk

package com.android.sharefres.main;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SharefresMainActivity extends Activity {
/** Called when the activity is first created. */

private String TAG="SharefresMainActivity";
private TextView mTextView;
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
try {
mContext=createPackageContext("com.android.sharefres.second",Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
mSharedPreferences=mContext.getSharedPreferences("device",MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
Log.i(TAG, mSharedPreferences.getInt("c", 0)+"=====mSharedPreferences==");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.main"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

次apk

package com.android.sharefres.second;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SharefresSecondActivity extends Activity {
/** Called when the activity is first created. */
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
mContext=getApplicationContext();
mSharedPreferences=mContext.getSharedPreferences("device",Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
Editor edit=mSharedPreferences.edit();
edit.putInt("c", 10);
edit.commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.second"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresSecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

注意两个apk都要有同一进程id即是:android:sharedUserId="com.android.sharefres"
同时注意,写文件的权限要为可读可写,否则才可以访问。

更多相关文章

  1. Android(安卓)安全机制
  2. Android高级进阶之路【六】Android(安卓)Framework解析
  3. Android: java应用程序进程启动过程的代码分析
  4. 分析Android(安卓)根文件系统启动过程(init守护进程分析)
  5. Android五个进程等级
  6. 转载:Android(安卓)获取ROOT权限原理解析
  7. 运行时权限解析以及申请的实现(可完美解决java.lang.SecurityExce
  8. Android各种访问权限Permission详解
  9. Android(安卓)IPC(进程间通信)

随机推荐

  1. 解析PHP观察者模式Observer
  2. FreeRTOS动态内存分配管理heap_5示例
  3. 浅谈PHP设计模式之对象池模式Pool
  4. Qt结合OpenCV部署yolov5的实现
  5. 实用代码3
  6. Android短信发送流程之长短信发送(原)
  7. Android(安卓)Service源码分析
  8. Android学习中,又遇到个不爽的bug
  9. Android配置----小米手机通过wifi连接ADB
  10. Android下PreferenceScreen 加载流程