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. 运行时权限解析以及申请的实现(可完美解决java.lang.SecurityExce
  2. 转载:Android 获取ROOT权限原理解析
  3. Android: java应用程序进程启动过程的代码分析
  4. Android跨进程通信-IPC初探(三) - 使用AIDL
  5. Android多进程模式

随机推荐

  1. Github 封禁了有伊朗外部贡献者的项目!
  2. 程序员又开始了疯狂的薅羊毛!
  3. 恒丰银行:金融云中 x86 裸机服务实践
  4. 重要 | mr使用hcatalog读写hive表
  5. 44 页的 JVM 2020 生态报告!
  6. 微信海量数据监控的设计与实践
  7. 必会:关于SparkStreaming checkpoint那些
  8. 从零搭建一个自动化运维体系
  9. 程序员在 Github 上疯狂薅羊毛!
  10. 打不赢,就封号!孙杨上诉翻案率仅约7%?