简体   繁体   English

Android:如何删除另一个包中的共享首选项

[英]Android : How to remove shared preferences in another package

In my android application I coded to read shared data of another Android application and then to delete that data from shared preferences. 在我的Android应用程序中,我编码读取另一个Android应用程序的共享数据,然后从共享首选项中删除该数据。 My code as follows : 我的代码如下:

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();
   }

This shows following error: 这显示以下错误:

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

I used this method in my other application to make shared data 我在我的其他应用程序中使用此方法来创建共享数据

 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();
}

How can I do that ? 我怎样才能做到这一点 ? Is there anything to add Manifest file ? 有没有什么可以添加清单文件?

Thanks! 谢谢!

If you use android:sharedUserId in your manifest files it should work. 如果你在清单文件中使用android:sharedUserId它应该工作。 This is a permissions issue I've been running into myself. 这是我自己遇到的权限问题。

To do this, you simply need to add a tag such as android:sharedUserId="com.example.you" to your <manifest> tag in your AndroidManifest.xml file for both of your applications (and the com.example.you has to be the same in both apps, of course). 要做到这一点,你只需要添加一个标签,如android:sharedUserId="com.example.you"你的<manifest>标签在你AndroidManifest.xml为您的应用程序文件(和com.example.you有当然,在两个应用程序中都是一样的)。

Example start of the manifest file: 清单文件的示例启动:

<?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" >
...

For an indepth description of the way to get this working see my answer on How can I share a SharedPreferences file across two different android apps? 有关如何使其工作的深入描述,请参阅我的答案如何在两个不同的Android应用程序之间共享SharedPreferences文件?

You need to use MODE_WORLD_READABLE instead of MODE_PRIVATE . 您需要使用MODE_WORLD_READABLE而不是MODE_PRIVATE Read the docs for further information. 阅读文档以获取更多信息。

Here's a tutorial to check further if you have any more mistakes. 这是一个教程 ,可以进一步检查你是否还有其他错误。

While the other solutions here will technically work in most circumstances, the framework that Android has provided to you for sharing data between processes/applications is the ContentProvider . 虽然此处的其他解决方案在技术上可以在大多数情况下使用,但Android为您提供的用于在进程/应用程序之间共享数据的框架是ContentProvider It may seem like a lot of extra abstraction, but it's the one that is guaranteed to work. 它可能看起来像是一个额外的抽象,但它是保证工作的那个。

While the interface for this component mirrors the calls into a database, the underlying data structure can be anything you like. 虽然此组件的接口将调用镜像到数据库中,但基础数据结构可以是您喜欢的任何内容。 In specific, you can return a MatrixCursor in response to queries that provides the contents of your SharedPreferences object, and you can implement a URI scheme for delete/update calls that can be used to modify the preferences from other applications. 具体而言,您可以返回MatrixCursor以响应提供SharedPreferences对象内容的查询,并且可以实现删除/更新调用的URI方案,该方案可用于修改其他应用程序的首选项。

Here is a link to a blog post from another developer who used MatrixCursor to share preferences. 以下是使用MatrixCursor分享偏好设置的其他开发人员发布的博客文章链接

HTH HTH

I've been struggling with SharedPreferences the last three days, but I think I finally solved it for myself. 过去三天我一直在努力使用SharedPreferences,但我想我终于为自己解决了这个问题。 Here's a few tips and gotchas you should try out that might help you (and loads of other stackoverflow users). 以下是您应该尝试的一些技巧和难题,可能对您有所帮助(以及其他堆栈溢出用户的负载)。

Please note that all my observations are for ICS (4.0.2 and 4.0.4). 请注意,我的所有观察结果都是针对ICS (4.0.2和4.0.4)。

  1. If you change the android:sharedUserId , delete the app as file/folder permissions will be incorrect. 如果您更改了android:sharedUserId ,请删除该应用程序,因为文件/文件夹权限将不正确。 So you shouldn't change that value if you have already relased the app. 因此,如果您已经重新启动应用程序,则不应更改该值。
  2. If you change the Context.MODE_* values, delete the app's data (or the whole app) to ensure file permissions aren't incorrect. 如果更改Context.MODE_*值,请删除应用程序的数据 (或整个应用程序)以确保文件权限不正确。
  3. If you use android:sharedUserId , be sure to sign the apps with the same certificate . 如果您使用android:sharedUserId ,请务必使用相同的证书对应用程序进行签名
  4. Use a different filename for the preferences in the two apps. 在两个应用程序中为首选项使用不同的文件名 I had problems with the app just reading the local shared preferences even though I used createPackageContex() . 即使我使用了createPackageContex()我在阅读本地共享首选项时也遇到了问题。

This is what worked for me in the end: 这最终对我有用:

  1. I used the same android:sharedUserId for the two apps. 我为这两个应用程序使用了相同的android:sharedUserId (Not the same android:process .) (不一样的android:process 。)
  2. I used Context.MODE_WORLD_READABLE for both reading and writing local prefs, and for reading the other app's prefs. 我使用Context.MODE_WORLD_READABLE读取和写入本地首选项,以及读取其他应用程序的首选项。
  3. I used Context.CONTEXT_IGNORE_SECURITY when calling createPackageContext() . 我在调用createPackageContext()时使用了Context.CONTEXT_IGNORE_SECURITY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM