首页 > 解决方案 > Android Chrome 自定义选项卡意图过滤器仅限于我的应用程序?

问题描述

我正在尝试在 Android 上实现https://mobikul.com/use-safariviewcontroller-callback-url-objective-c/,其中在 iOS 示例中检查了“sourceApplication”,并且只允许 SFSafariViewController 重定向回应用程序(大概它是由应用程序实例化的 SFSafariViewController ......但这是另一个问题!)。

我无法在 Android 中实现平等。在我的应用程序中,BrowserActivity打开一个 Chrome 自定义选项卡,它CallbackActivity从 HTML 重定向回。我发现,如果我输入正确的 URL 架构,Chrome / 手机上的任何浏览器也可以重定向回,这是个坏消息!CallbackActivity清单中似乎应该有一个白名单机制,只允许从我的应用程序打开的 Chrome 自定义选项卡重定向回我的活动,而不允许其他任何人。我无法在清单中设置android:exported="false"CallbackActivity因为这会破坏我的应用程序的重定向功能。如何实现这一目标?

这是我为 Android 清单准备的内容:

    <activity android:name="com.epicgames.ue4.SplashActivity" android:launchMode="singleTask" android:debuggable="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.epicgames.ue4.GameActivity" android:exported="false"> 
      <meta-data android:name="android.app.lib_name" android:value="UE4" />
    </activity>
    <activity android:name=".BrowserActivity" android:launchMode="singleTask" android:exported="false">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
      </intent-filter>
    </activity>
    <activity android:name=".CallbackActivity" android:launchMode="singleTask">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="customscheme" android:host="callback" />
      </intent-filter>

以下是我对相关活动的了解:

public class BrowserActivity extends Activity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        final Uri uri = Uri.parse(getIntent().getStringExtra("url"));

        final CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
        intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.intent.setData(uri);

        startActivity(intent.intent);
    }
}

public class CallbackActivity extends Activity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        //TODO validate only chrome custom tab HTML, started from my app, redirected here
        //getCallingPackage() is always null and referrer host can be spoofed...how to?

        final Intent intent = new Intent(this, com.epicgames.ue4.GameActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
 
        startActivity(intent);
    }
}

标签: androidintentfilterchrome-custom-tabs

解决方案


推荐阅读