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

时间:2015-02-07 08:22:55

标签: android

我有两个申请..

一个用于主要应用..另一个用于标题转换器..

如果我在一个应用程序中输入标题,则会影响另一个应用程序..

我尝试了什么,

我已经通过SD卡中的商店数据库完成了...并使用DBHelper引用标题。

我的问题,

但现在我的数据库已移入主应用程序..因此我无法从标题转换器应用程序中引用数据库。

我的问题,

如果没有数据库创建,我可以在两个应用程序之类的意图或共享首选项之间传递数据吗?

注意:两个应用程序都存储在同一设备中..

3 个答案:

答案 0 :(得分:1)

您有多种方法可以实现这一目标。

  1. Content Providers
  2. 的用法
  3. MODE_WORLD_WRITEABLE
  4. 的共享偏好设置
  5. 每当在一个应用程序中更改内容时,发送一个广播意图,可以使用其他应用程序使用接收方读取。

答案 1 :(得分:1)

从应用程序1发送数据(例如:应用程序1包名称为" com.sharedpref1")。

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

接收应用程序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());
         }

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

 android:sharedUserId="any string" 
 android:sharedUserLabel="@string/any_string"

两者都相同......共享用户标签必须来自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">

答案 2 :(得分:-1)

您可以使用意图。 您可以将活动中的意图发送到另一个应用程序中的活动,并将数据作为&#34; Extras&#34;在发送意图中。 您必须创建一个使用默认类别的自定义操作的新活动。 Chech Android Intents - Android Developers