4

我正在將PayPal實施到我當前的Android應用中,並且我被建議使用Chrome自定義選項卡,但似乎無法觸發Intent。Chrome自定義選項卡 - 意圖未被觸發(Android)

我相信我已經在AndroidManifest.xml

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

正確設置了意向,我能夠成功地啓動谷歌瀏覽器選項卡在我的片段類(如果我有2個按鈕:

  • 將在Chrome自定義選項卡
  • 一個按鈕啓動我的自定義網頁推出www.google.co.uk A鍵(託管在本地主機上),當用戶點擊一個按鈕/鏈接,將拋出重定向

請參閱下面的代碼對該片段:

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"); 
     } 
    } 
} 

任何幫助都將不勝感激!

+0

對於任何試圖使用'http:// localhost:8200'作爲'redirectUrl'的用戶授權...您無法在'Chrome Custom Tabs'中使用'localhost' ...您需要定義您自己的自定義網址方案。如果您只是Android開發人員,您可能需要讓後端開發人員在他們的最後包含此更改。 – Sakiboy

回答

7

我認爲你的計劃和主機混在一起。自定義方案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,它顯示了一個Chrome的自定義選項卡被用於執行OAuth流(對Google的OAuth服務器),,包括解析來自返回意圖的數據。這是演示中的AndroidManifest.xml