首先,两个应用要有相同的sharedUserId:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.text.uid"
android:sharedUserId="com.text.jzh">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
应用A 中存储数据:
SharedPreferences sp = getSharedPreferences("text", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("text","what the fuck");
editor.commit();
应用B 中获取数据:
try {
//创建应用A的context
Context context = createPackageContext("com.example.text", CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = context.getSharedPreferences("text", Context.MODE_PRIVATE);
Toast.makeText(getApplicationContext(),sp.getString("text","find null"),Toast.LENGTH_SHORT).show();
}catch (Exception e){
}
这样在应用B中就可以获取到应用A 的数据了