【问题标题】:Unable to pass value from one application to another application无法将值从一个应用程序传递到另一个应用程序
【发布时间】:2018-12-22 17:55:06
【问题描述】:

我在不同的设备上有两个独立的 Android 应用程序。尝试传递或共享目标地址(但在这种情况下,我们将尝试一个字符串)。

如果我在同一台设备上运行这两个应用程序,它就可以工作。但是,如果我在设备上运行一个应用程序并在模拟器中运行另一个应用程序,则 App2 中的结果会显示“Hello World”或“No Value”并且不是“我们很棒!”就像它应该正确传递或共享一样。

有什么想法吗?

请看代码:

App1:com.example.sharedpreferences

MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences prefs = this.getSharedPreferences("demopref", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("demostring", "We are great ");
    editor.apply();
  }
}

activity_main (app1)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sharedpreferences"
android:sharedUserId="@string/dev"
android:sharedUserLabel="@string/dev1">
.......

strings.xml

<string name="dev">com.example</string>
<string name="dev1">example</string>

App2:com.example.sharedprefs2

activity_main (app2)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

strings.xml

<string name="dev">com.example</string>
<string name="dev1">example</string>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sharedprefs2"
android:sharedUserId="@string/dev"
android:sharedUserLabel="@string/dev1">

<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>

        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

</manifest>

MainActivity

public class MainActivity extends AppCompatActivity {

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = findViewById(R.id.tv);

    Context con = null;

    try {
        con = this.createPackageContext("com.example.sharedpreferences", 0);
        SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
        String your_data = pref.getString("demostring", "No Value");
        tv.setText(your_data);
        Log.d(TAG, "text passed in = " + your_data);
    } catch (Exception e) {
        Log.e("Not data shared", e.toString());
    }
}
}

我该如何解决这个问题?

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    我认为您误解了 SharedPreferences 是什么以及它们是如何工作的。

    它们仅将数据本地存储在您的设备上。它们不能以任何方式与其他设备共享。

    因此,您可以按照在同一设备上的 2 个应用之间发布的方式共享它们。但是,如果它跨多个设备,这绝不会起作用。

    为此,您需要一些后端服务。不妨看看 Firebase 数据库。

    【讨论】:

    • 除了内容提供者之外,我有什么建议可以让它工作吗?我已经尝试了几乎所有我能想到的东西。意图、共享首选项、广播接收器等。什么类型的后端服务?我已经有了这些应用程序的 Firebase 数据库。通知会在 2 个设备之间传递值吗?
    • Intents、SharedPrefs、Broadcast 接收器,所有这些都仅在设备上。使用 Firebase 数据库,您可以让两个应用程序监听相同数据的更改,并正确同步它们。然后 1 个应用程序更新数据,而另一个应用程序读取数据。
    • 那行得通,我该怎么做呢?我在 Firebase 上的一个项目中同时拥有这两个应用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2013-05-19
    • 1970-01-01
    • 2021-06-26
    • 2013-01-16
    • 1970-01-01
    相关资源
    最近更新 更多