android - 如何在不同的android应用程序之间双向传递数据?

标签 android android-intent android-activity

将字符串变量从一个应用程序传递到另一个应用程序并返回值的最简单方法是什么?我可以访问这两个应用程序的源代码,但它必须是两个不同的应用程序。

我尝试使用 startActivityForResult,但这似乎只适用于同一应用程序的 Activity 。从不同的包调用 Activity 时,startActivityForResult 会立即返回 RESULT_CANCELED。似乎有可能通过服务来解决这个问题,但对于一些字符串变量来说,这不是有点过大了吗?

有没有简单干净的方法来做到这一点?

这里是我尝试用于 startActivityForResult 的代码:

//App A:
            Intent intent = new Intent();
            intent.setAction("com.example.testapp.MESSAGE");
            Bundle b = new Bundle();
            b.putString("loginToken", "263bhqw3jhf6as4yf8j0agtz8h2hj2z9j3hg3g3ggh34uzh2h2ui78h3i9wdnj89x");
            intent.putExtra("MyData", b);

            startActivityForResult(intent, TEST_REQUEST);

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("pairing", "onActivityResult called");
    // Check which request we're responding to
    if (requestCode == TEST_REQUEST) {
        // Make sure the request was successful
        Log.d("pairing", "got result, resultCode: " + resultCode);
        if (resultCode == RESULT_OK) {
            // The Intent's data Uri identifies which contact was selected.
            if (data.hasExtra("returnMessage")) {
                Toast.makeText(this, data.getExtras().getString("returnMessage"), Toast.LENGTH_LONG).show();
            }

        }
    }
}


            // App B:
        Intent result = new Intent();
        Bundle b = new Bundle();
        b.putString("returnValue", "this is the returned value");
        result.putExtra("MyData", b);
        setResult(Activity.RESULT_OK, result);
        Log.d("pairing", "RESULT_OK set");
        finish();


//App B Manifest
        <activity
        android:name="com.example.testapp"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" >
        <intent-filter>
            <action android:name="com.example.testapp.MESSAGE" />

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

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

有人看到错误了吗?应用程序 B 总是立即返回 RESULT_CANCELED

编辑: 现在我收到一个 android.content.activitynotfoundexception no activity found to handle intent { act=com.example.testapp.MESSAGE (has extras) } 错误。我做错了什么?

最佳答案

AIDL 是使用接口(interface)在两个不同应用程序之间进行通信的一种方式

http://developer.android.com/guide/components/aidl.html

您可以在下面的教程中找到工作示例 http://manishkpr.webheavens.com/android-aidl-example/

关于android - 如何在不同的android应用程序之间双向传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18966016/

相关文章:

android - 出现软键盘时布局不会向上推

android - 如何在运行时获取准确的ListView高度?

android - 如何设置自定义zip文件的mime类型?

android - 为什么 Android 文档说 Intent Extras 需要包前缀

java - 如何更改图像并在 5 秒后重新运行线程中的主要 Activity 后不返回到 android 中的基本布局

android - 手动预编译xml布局文件并使用

Android - ES 文件资源管理器的共享 Intent

android - 在 Android Activity 类中存储数据数组

android - 已激活时终止 Activity

java.lang.NullPointerException : Attempt to invoke virtual method . .. 在空对象引用上