代码之家  ›  专栏  ›  技术社区  ›  LizG

无法将值从一个应用程序传递到另一个应用程序

  •  0
  • LizG  · 技术社区  · 7 年前

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

    如果我在同一台设备上运行这两个应用程序,它就会工作。但是,如果我在设备上运行一个应用程序,在模拟器中运行另一个应用程序,则app2中的结果将显示“hello world”或“no value”,并且 “我们很棒!”如果传递或共享正确,则应该如此。

    你知道为什么吗?

    请参见代码:

    应用程序1:com.example.sharedpreferences

    主要活动

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

    主要活动(附录1)

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

    StrugsXML

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

    应用程序2:com.example.sharedprefs2

    主要活动(附录2)

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

    StrugsXML

    <string name=“dev”>com.示例</string>
    <string name=“dev1”>示例</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>
    

    主要活动

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

    我怎么解决这个问题?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Moonbloom    7 年前

    我想你误解了什么是共享引用以及它们是如何工作的。

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

    所以你可以在同一设备上的2个应用程序之间共享它们。但如果是跨多个设备的话,这根本不起作用。

    为此,你需要一些后端服务。或许可以看看firebase数据库。