android - Chrome 自定义标签 - Intent 未被触发 (Android)

标签 android android-intent paypal url-redirection chrome-custom-tabs

我正在努力将 PayPal 实现到我当前的 Android 应用程序中,有人建议我使用 Chrome 自定义选项卡,但我似乎无法触发 Intent。

我相信我已经在 AndroidManifest.xml 中正确设置了 Intent

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rhiannon.chromecustomtabs">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".CompletePayPalPaymentActivity">
            <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:scheme="Test"
                android:host="com.Test.TestApp.PayPalReturn"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

我能够在我的 Fragment 类中成功启动 Google Chrome 标签页(我有 2 个按钮:

  • 将在 Chrome 自定义标签页中启动 www.google.co.uk 的按钮
  • 启动我的自定义网页(托管在本地主机上)的按钮,当用户单击按钮/链接时将抛出重定向

请看下面这个 fragment 的代码:

public class MainFragment extends Fragment implements CustomTabsSceneHelper.ConnectionCallback {

    public enum CustomTabsAction {
        GOOGLE ("Open Google", "http://google.co.uk"),
        REDIRECT ("Open Redirect", "http://{mylocalhost_ip}/~Rhiannon/example1/test.html");

        private final String mActionDescription;
        private final String mActionUrl;

        CustomTabsAction (
                final String actionDescription,
                final String actionUrl) {

            mActionDescription = actionDescription;
            mActionUrl = actionUrl;
        }

        public String actionDescription () {
            return mActionDescription;
        }

        public String actionUrl () {
            return mActionUrl;
        }
    }

    private CustomTabsSceneHelper mCustomTabsSceneHelper;
    private CustomTabsAction mCurrentAction;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCustomTabsSceneHelper = new CustomTabsSceneHelper();
        mCustomTabsSceneHelper.setConnectionCallback(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        Button buttonGoogle = (Button) view.findViewById(R.id.button_google);
        buttonGoogle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open(CustomTabsAction.GOOGLE);
            }
        });

        Button buttonRedirect = (Button) view.findViewById(R.id.button_redirect);
        buttonRedirect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open(CustomTabsAction.REDIRECT);
            }
        });

        return view;
    }

    private void open(CustomTabsAction action) {
        final Activity scene = getActivity ();
        mCurrentAction = action;

        CustomTabsSceneHelper.openCustomTab(
                scene,
                getCustomTabIntent(scene, mCustomTabsSceneHelper.occupySession()).build(),
                Uri.parse(action.actionUrl())
        );
    }

    public static CustomTabsIntent.Builder getCustomTabIntent(
            @NonNull Context context,
            @Nullable CustomTabsSession session) {

        // Construct our intent via builder
        final CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder (session);
        // Toolbar color
        intentBuilder.setToolbarColor(Color.GREEN);
        // Show title
        intentBuilder.setShowTitle(true);
        // Allow hiding for toolbar
        intentBuilder.enableUrlBarHiding();

        return intentBuilder;
    }

    @Override
    public void onCustomTabsConnected() {
        Log.i("MainFragment", "Custom Tabs > CONNECTED");
        if(mCurrentAction != null){
            mCustomTabsSceneHelper.mayLaunchUrl(Uri.parse(mCurrentAction.actionUrl()), null, null);
        }
    }

    @Override
    public void onCustomTabsDisconnected() {
        Log.i("MainFragment", "Custom Tabs > DISCONNECTED");
    }

    @Override
    public void onStart() {
        super.onStart();
        mCustomTabsSceneHelper.bindCustomTabsService(getActivity());
    }

    @Override
    public void onStop() {
        mCustomTabsSceneHelper.unbindCustomTabsService(getActivity());
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mCustomTabsSceneHelper.setConnectionCallback(null);
        super.onDestroy();
    }

}

然后我有我的自定义网页:

<!DOCTYPE html>
<html>
<head>
    <title>PayPal Test Redirect</title>
    <script type="text/javascript">
        function RedirectSuccess(){
            window.location="Test://com.Test.TestApp.PayPalReturn://return";
        }

        function RedirectCancel(){
            window.location="Test://com.Test.TestApp.PayPalReturn://cancel";
        }
</script>
</head>
<body>
<a href="Test://com.Test.TestApp.PayPalReturn://return">Success</a>
<a href="Test://com.Test.TestApp.PayPalReturn://cancel">Cancel</a>

<input type="button" onclick="RedirectSuccess();" name="ok" value="Success"  />
<input type="button" onclick="RedirectCancel();" name="ok" value="Cancel"  />
</body>
</html>

我希望当用户在 Chrome 自定义选项卡中点击网页上的链接/按钮之一时,用户将被重定向回应用程序,但我似乎无法使此功能正常工作。

public class CompletePayPalPaymentActivity extends AppCompatActivity {

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

        List<String> params = getIntent().getData().getPathSegments();
        if(params.get(0).equals("return")){
            Log.i("CompletePP", "PayPal Payment Success");
        }else{
            Log.i("CompletePP", "PayPal Payment Cancelled");
        }
    }
} 

任何帮助,将不胜感激!

最佳答案

我认为您的方案和主机混淆了。自定义方案 URI 没有主机!所以试试这个:

AndroidManifest.xml

    <activity android:name=".CompletePayPalPaymentActivity">
        <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="com.Test.TestApp.PayPalReturn"/>
        </intent-filter>
    </activity>

那么您的 HTML 链接将是:

<a href="com.Test.TestApp.PayPalReturn:/cancel">Cancel</a>

有关工作示例,请查看 AppAuth for Android ,它显示了用于执行 OAuth 流程(到 Google 的 OAuth 服务器)的 Chrome 自定义选项卡,包括从返回 Intent 中解析数据。这是 AndroidManifest.xml来自那个演示。

关于android - Chrome 自定义标签 - Intent 未被触发 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008514/

相关文章:

android - 在 Android 的 Horizo​​ntalScrollView 中动态插入 View

ios - Paypal SDK 集成问题

php - PayPal 快速结帐页面中不允许使用非英语字符

java - 在 app/build.gradle 中更改 applicationId 后获取 ClassNotFoundException?

Android Intent Share 使用外部图片 url

android - 下载文件超时

java - 允许用户从画廊中选择图像以上传到 Activity 中的 ImageView ?

.net - Payflow Pro .Net SDK (Payflow_dotNET.dll) 缺少查询交易命令的 CUSTREF 选项

android - 创建新 View 时动态隐藏 View stub

javascript - $http 在异步方法中不起作用 - AngularJS 和 PhoneGap