2

我有两个应用程序。第一个 A 有一个 Web 视图,其中包含指向另一个应用程序的链接和一个时间戳。在这个 web 视图中,我有一个打开另一个应用程序 B 的链接。在 B 中,我有一个按钮。单击该按钮应将我带回应用程序 A,无需重新加载它,并执行应填充 Web 视图中的其他一些字段的 javascript 代码。因此,应用程序 A 应该被带回,时间戳不应该被更改,并且调用的 javascript 和正确填充的字段。

问题是,如果我执行此方案并且仅使用 StartActivity() 从 A 启动应用程序 B,则它不起作用。我可以做 AB 和 BA,但我的 javascript 没有被调用,因为 A 没有以正确的意图被调用。换句话说,当从 B 调用 A 时,我应该在意图对象中找到将我带到 A 的动作和数据,但意图中的动作是 NULL

另一方面,如果在 B 上启动 Activity 时添加一个标志:FLAG_ACTIVITY_NEW_TASK。它工作一次。A->B->A 但在那之后我不能再给 B 打电话了。我进行的另一个测试:如果我从主屏幕开始呼叫 B,然后呼叫 A,然后呼叫 B。它有效。所以我想这与如果使用来自 A 或从主屏幕的意图调用 B 是不同的事实有关。

无论如何,我无法让它打开应用程序,调用 js 并同时保持上下文。

我只在模拟器上试过。

这是应用程序 A 的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.cgi.csb.launcher"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name=".Activity1" launchMode="singleTask">
            <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.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:host="localhost" android:scheme="myactivity"></data>
            </intent-filter>
        </activity>
    </application>
</manifest>

这是 App B 的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.CallerApp"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:label="@string/app_name">
        <activity android:name=".Activity2" launchMode="singleTask">
            <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.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data
                        android:host="dummy"
                        android:scheme="testintentapp"/>
            </intent-filter>
        </activity>

    </application>
</manifest>

从 A 呼叫 B :

@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                intent.addCategory("android.intent.category.DEFAULT");
                startActivity(intent);

                return true;
            }

从 B 调用 A :

case R.id.button:
                   Intent intent = new Intent();
                   intent.setData(Uri.parse("myactivity://localhost"));
                   intent.putExtra("firstKeyName","FirstKeyValue");
                   intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                   startActivity(intent);
                   finish();
                   break;

我用来区分何时从家里调用 A 或从 App B 调用 javascript 的代码

    Intent intent = getIntent();

    if ("myactivity://localhost".equals(intent.getDataString())) {
        Log.d(TAG, "*** Call JS with action : " + intent.getAction());
        _jsHandler.javaFnCall("Hardcoded params");
    } else {
        Log.d(TAG, "***Just call the app with action :" + intent.getAction());
    }

非常感谢 !

4

0 回答 0