两个应用程序之间的数据共

Rah*_*rma 38 android

最近我接受了一次采访,其中问题是"您如何能够在两个已安装的应用程序或apk之间共享数据?"

我对这个问题没有任何答案.任何人都可以帮我确定一种方法吗?

Rof*_*ion 28

ContentProviders是在应用程序之间共享数据的好方法.

  • 谢谢你的链接.很抱歉,您可以更具体地了解如何共享数据.任何例子都会有很大的帮助 (3认同)
  • @RanjithKumar - 好抓!我没有尝试过,但这似乎是来自不同来源的相同样本:[概述](https://newcircle.com/s/post/64/contentprovider_user_demo)并下载:[mirror 1](https: //thenewcircle.com/static/tutorials/ContentUserDemo.zip)和[mirror 2](https://dl.dropboxusercontent.com/u/274922/ContentUserDemo.zip) (2认同)

Ran*_*mar 28

从Application 1发送数据(例如:Application 1包名称为"com.sharedpref1").

SharedPreferences prefs = getSharedPreferences("demopref",
                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("demostring", strShareValue);
            editor.commit();
Run Code Online (Sandbox Code Playgroud)

接收应用程序2中的数据(从应用程序1中的共享首选项获取数据).

    try {
            con = createPackageContext("com.sharedpref1", 0);//first app package name is "com.sharedpref1"
            SharedPreferences pref = con.getSharedPreferences(
                        "demopref", Context.MODE_PRIVATE);
            String your_data = pref.getString("demostring", "No Value");
        } 
    catch (NameNotFoundException e) {
                Log.e("Not data shared", e.toString());
         }
Run Code Online (Sandbox Code Playgroud)

在两个应用程序清单文件中添加相同的共享用户ID和标签,

 android:sharedUserId="any string" 
 android:sharedUserLabel="@string/any_string"
Run Code Online (Sandbox Code Playgroud)

两者都相同......共享用户标签必须来自string.xml

像这个例子.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="any string" 
android:sharedUserLabel="@string/any_string">
Run Code Online (Sandbox Code Playgroud)

  • 你不必用相同的密钥签署应用程序吗? (12认同)
  • 很好地解释了。它的工作。谢谢哥们儿 :-) (2认同)
  • 工作完美! (2认同)

Mik*_* dg 5

我怀疑他们可能正在寻找 Android 特定的方法,例如内容提供商的答案。

其他替代方案... Android 特定 - 远程服务 常规 - TCP/IP 连接 常规 - 写入 SD 卡上的某个位置

想了解更多有关特定方法的信息吗?

今天也偷了这个问题来接受采访:)


小智 5

如果要在应用程序之间共享数据,请确保使用相同的密钥签名:

通过权限共享代码/数据– Android系统提供了基于签名的权限实施,因此应用程序可以向使用指定证书签名的另一个应用程序公开功能。通过使用相同的证书对多个应用程序签名并使用基于签名的权限检查,您的应用程序可以安全方式共享代码和数据。

引自:关于签名的android开发人员页面

如果数据量很少,则可以通过意图将其发送。

  • 你能指出如何从一个应用程序获取或检索数据到另一个应用程序吗?该链接更多的是关于签名。 (2认同)