开发者

Starting an ACTION_VIEW activity to open the browser, how do I return to my app?

开发者 https://www.devze.com 2023-02-16 00:00 出处:网络
In my app i need to open the bank\'s page, to make the user able to pay. Reading the Android documentation I see that I should use an ACTION_VIEW (and not a WebView) to accomplish this.

In my app i need to open the bank's page, to make the user able to pay. Reading the Android documentation I see that I should use an ACTION_VIEW (and not a WebView) to accomplish this.

 Uri uri = Uri.parse("http://www.example.com");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

My question is: After the user is done with the payment, how can i get back to the app?

I m开发者_运维知识库ean, I'd like to do something like

startActivityForResult(intent, RESULT_CODE);

to open the bank's site, and then get back to the app when the user is done, using the

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

callback to handle the result of the payment.

And, am I following the right way? Or is there any other way to accomplish this?


How would the browser know that user is done with the bank page? It has no way to know that.

Also, browser does not react to startActivityForResult() by setting result and then finish().

So, you can not use Android browser to accomplish this task.

The only possible way would be to start WebView and to detect when user is finished, by detecting certain url that is (supposedly) shown when user is finished with the bank task.


Actually you are able to return to your calling activity taking a different approach. If the webpage you are calling is handled by you or allows you to assign a callback when the user finishes doing dome processing, then you can set that callback to a URI, and assign that URI to your calling activity, if the activity launch mode is singleTask, then you're going to be able to resume your activity.

The only thing that I haven't resolved is that I don't know how to remove the browser from the stack once it returns to your calling activity. I want to accomplish this because once you leave your app, android pops the browser activity to the front, forcing the user to finish it by himself.


The Application cannot find out if user has finished with ban page or not. usually bank payment gateways called with a callBackUrl attribute.

i handle this situation like this:

  1. on my callBackUrl i set a url to verify payment page (Web page page)
  2. in verify payment page`s html, i made a button labeled as "Return To Application" this Button href is a url like this "/app/order/payed"
  3. in my Android application i make use of Android App Link

Html Page:

<div class="flex-center position-ref" style='height:30vh;'>
          <a href="http://myserver/app/order/payed" class="btn-success">
            Return To Application
          </a>
        </div>

in Android Manifest:

<activity
            android:name=".shop.customer.order.OrderPayedActivity"
            android:launchMode="singleInstance"
            android:taskAffinity=".shop.customer.cart.CartActivity">
            <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:host="@string/BASE_URL_HOST"
                    android:pathPattern="/app/order/payed"
                    android:scheme="@string/BASE_URL_SCHEME" />
            </intent-filter>
        </activity>

and in The Receiving Activity:

//managed parent activities in the manifest in order to make task`s back stack 
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this,OrderListActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
    taskStackBuilder.addNextIntentWithParentStack(intent);

    taskStackBuilder.startActivities();
    //startActivity(intent);
    finish();
}


Just put a ResultCode argument on your startActivityForResult method, something like =this.startActivityForResult(intent, PAYMENT_ACCEPTED)

It will be returned as resultCode when you finish your second activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消