简体   繁体   English

Chrome自定义标签 - 意图未被触发(Android)

[英]Chrome Custom Tabs - Intent not being triggered (Android)

I am working on implementing PayPal into my current Android app, and I have been advised to use Chrome Custom Tabs, but I cannot seem to get the Intent to be triggered. 我正在努力将PayPal应用到我当前的Android应用程序中,并且我被建议使用Chrome自定义标签,但我似乎无法触发Intent。

I believe I have set the Intent up correctly in the AndroidManifest.xml 我相信我已经在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>

I am able to successfully launch the Google Chrome Tabs in my Fragment class (Where I have got 2 buttons: 我能够在Fragment类中成功启动Google Chrome Tabs(我有2个按钮:

  • A button that will launch www.google.co.uk in a Chrome Custom Tab 一个按钮,可在Chrome自定义标签中启动www.google.co.uk
  • A button that launches my custom webpage (hosted on localhost) that will throw the redirects when the user clicks a button/link 一个按钮,用于启动我的自定义网页(托管在localhost上),当用户单击按钮/链接时,该按钮将引发重定向

Please see below the code for this 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();
    }

}

I then have my custom webpage: 然后我有我的自定义网页:

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

I would expect that when the user clicks on one of the links/buttons on the webpage in the Chrome Custom Tab that the user would be redirected back to the application, but I cannot seem to get this functionality to work. 我希望当用户点击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");
        }
    }
} 

Any help at all, would be greatly appreciated! 任何帮助,将不胜感激!

I think your scheme and host is mixed up. 我认为你的计划和主持人混在一起。 Custom scheme URIs don't have hosts! 自定义方案URI没有主机! So try this instead: 所以试试这个:

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>

Then the link your HTML would be: 然后你的HTML链接:

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

For a working example, checkout AppAuth for Android , it shows a Chrome Custom Tab being used to perform an OAuth flow (to Google's OAuth servers), including parsing data from a return intent. 对于一个工作示例,结帐AppAuth for Android ,它会显示一个Chrome自定义标签,用于执行OAuth流程(到Google的OAuth服务器), 包括解析返回意图中的数据。 Here's the AndroidManifest.xml from that demo. 这是该演示的AndroidManifest.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM