在我的 android 应用程序中,我编写代码以读取另一个 Android 应用程序的共享数据,然后从共享首选项中删除该数据。 我的代码如下:
try {
con = createPackageContext("com.testapp.ws", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
ipAdr = pref.getString("demostring", "No Value");
pref.edit().remove("demopref").commit();
}
这显示了以下错误:
06-12 11:52:07.400: E/ApplicationContext(3587): Couldn't rename file /data/data/com.testapp.ws/shared_prefs/demopref.xml to backup file /data/data/com.testapp.ws/shared_prefs/demopref.xml.bak
我在我的其他应用程序中使用了这种方法来制作共享数据
public void shareData(){
String strShareValue = ip;
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
}
我该怎么做? Manifest 文件有什么需要添加的吗?
谢谢!
最佳答案
如果您使用 android:sharedUserId在您的 list 文件中它应该可以工作。这是我自己遇到的权限问题。
为此,您只需添加一个标签,例如 android:sharedUserId="com.example.you"
给你的<manifest>
在你的 AndroidManifest.xml
中标记两个应用程序的文件(当然,两个应用程序中的 com.example.you
必须相同)。
list 文件的示例开头:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package.name"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.example.you" >
...
有关实现此工作的方式的深入描述,请参阅我在 How can I share a SharedPreferences file across two different android apps? 上的回答
关于安卓 : How to remove shared preferences in another package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11020853/