StartActivityForResult from browser - android

In Android, if you want to launch an activity and have that activity return a result you do something like this:
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivityForResult(intent);
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
}
If you want an activity to be launched from the browser you can write some HTML like this:
Link
Question:
Can you launch an activity from the browser and have that activity return a result to the browser?
I don't think it's possible but if anyone has any idea, please let me know. If you have an source that explain how to do this, I am happy to make a POC and report my findings.

basically its not possible
since followings concerns are not clear.
How will you handle results.
Where you where handle results.

StartActivityForResult from browser is not possible.
As per the chrome developer guide, the basic syntax for an intent-based URI is as follows:
intent:
HOST/URI-path // Optional host
#Intent;
package=[string];
action=[string];
category=[string];
component=[string];
scheme=[string];
end;
It do not provide any callback parameter, in which you can get callback, so it is not feasible.
For more details visit https://developer.chrome.com/multidevice/android/intents

It isn't possible (as others have stated). However, you could have your Activity launch an URL in the browser which could then show the "result". This would be a way to have your Activity "return a result" to the browser. The URL launched could be static HTML page, or some JavaScript code or whatever.

Related

Android: calling Activities explicitly using Intent Url scheme

im trying to understand how Intent URL's working in android . i know that i can make implicit call to activities which has a intent-filter set in AndroidManifest file . but as far I've read we can call application Activities which has no intent-filter set in AndroidManifest . i tried some cases but i couldn't make it happen . i wrote a simple application with some Activities (no intent-filter) but i couldn't run them using Intent URL . and i couldn't find any useful example on explicit intent url's in the internet (and stackoverflow) .
so i'd be thankful if anyone could provide a simple exmple for explicit Activity calls using intent url's or a brief explanation .
Regards,
Mohammad
This is a little late for this answer, But for future reference, I'm thinking by Intent Url you mean the Uri of your intent.
There is an easy to find the Uri format of any intent (explicit or implicit) just by declaring an Intent object with those parameters in java and then calling the toUri method which gives you the String format of that intent. You can then use it in your web page to communicate with your app.
Intent intent = new Intent(this, SomeActivity.class);
String intentUri = intent.toUri(0);

onActivityResult equivalent in Delphi XE5?

In android/Java I would do this:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
and get the result in:
#Override public void onActivityResult(int reqCode, int resultCode, Intent data)
In Delphi I have the equivalent of the first part:
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_PICK);
Intent.setType(TJContactsContract_Contacts.JavaClass.CONTENT_TYPE);
MainActivity.startActivityForResult(Intent,PICK_CONTACT);
But in looking under MainActivity I don't see a "onActivityResult" callback. I see one for JFragment but don't seem to be in the right spot to get it and I see no examples in the example code, online or in the source code.
Currently you have to subclass the compiled Java class that acts as the entry point Activity on the Java side, where you can override onActivityResult().
This is rather messy and requires de-dexing classes.dex to get the current NativeActivity subclass, that you must in turn subclass. You must also modify the manifest and replace the normally deployed classes.dex with a new one that has your subclass in it.
You may sense that this is rather messy, which is why it may be best to hold off until they add in a hook to get activity results, which should be coming along soon, hopefully...
This is a good review, I do not know if it work the same like Intent.
http://www.pclviewer.com/android/androidJNI.html
http://www.pclviewer.com/android/XE5.pdf

Get Back Result from Android native Calculator

I'm invoking android native calculator from my app, how do i get result data from it.. means i started native calender like this, after finishing calculation i press back onActivityResult is executed and data returned is null, how to get calculated data.. Help me
Intent i = new Intent();
i.setClassName("com.android.calculator2",
"com.android.calculator2.Calculator");
startActivityForResult(i, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
Log.i("CALCULATOR", "Result Data is"+ data);
}
}
After some testing, I'm starting to think that you can't really get something back from the calculator. Calling something with startActivityForResult doesn't mean it's going to return something other than null and since there's no way of getting out of the calculator other than pressing the back key, I think this is one of those cases.
The native calculator doesn't seem to be calling setResult(RESULT_SUCESS,intent_with_data) which is the step needed to be able to retrieve this result. Easiest thing I can think of, since you're wanting to do some calculation is to implement your own calculator class and call that one instead of the native one.
Calculators are easy to make and you have a zillion examples on the net. Just make sure you have an OK button that calls setResult(RESULT_SUCESS, intent_with_data) after you put extras to the intent with the result.
Warning
Be aware that you're hardcoding a class name instead of calling an intent by specifying an action and URI. This may call the original calculator on the emulator and standard versions of Android, but manufacturers change those kinds of things and since no one is supposed to be calling them like you intend to with your intent, you may end up crashing the app.

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

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 mean, 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:
on my callBackUrl i set a url to verify payment page (Web page page)
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"
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.

onActivityResult doesn't work?

I am facing with a problem related startActivityForResult()
To start SecondActivity from FirstActivity :
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("key1", "12345");
startActivityForResult(intent, 0);
And handles result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
}
To send the message to FirstActivity from SecondActivity :
in SecondActivity :
setResult(0);
I can't handle the result on onActivityResult in FirstActivity.
It never works for my application.
My OS is : 1.5
What is wrong here?
startActivityForResult is meant to be used for situations where you want to select a piece of data, or perform some sort of action that your Activity or application cannot do.
For example, you want to pick a contact, so you launch the contacts application, the user chooses the person they want, and you get sent the result. Or you want to take a photo, so you launch the camera application and ask it to send you the photo once it's done. This action is completely separate from your first activity that calls startActivityForResult.
The Activity you're launching will not send you the result until that Activity has completed, i.e. finish() has been called.
So in your case, you need to call this in SecondActivity:
setResult(...);
finish();
before FirstActivity will receive the result in its onActivityResult method. Of course, this means that SecondActivity is now gone and FirstActivity is top of the stack again.
It's not possible to send the result to FirstActivity then close it while keeping SecondActivity still active. In this case you should just handle whatever this 'result' is in SecondActivity, or send it off to a Service you define to do whatever processing it is you want.
I was stuck here for a while. Adding my problem here to make sure that you don't scratch your head as well.
The second parameter of this function has to be 0 or higher.
startActivityForResult(intent, 0); // <- this is OK
I was setting the second parameter to RESULT_OK, which is -1, and my onActivityResult callback was never getting called. So if you get stuck like me, you can also check if your second parameter is correct.
startActivityForResult(intent, RESULT_OK); // <- this is wrong
The above line will fail to call onActivityResult.
I was also stuck on the same problem - but due to a different reason as matangs. Apparently startActivityForResult only works if you have android:launchMode set to standard for main activity (in manifest). Hope it helps someone.
Your code seems ok, but do you stop your second activity ?
Try this in it :
setResult(0);
finish();
If you are doing actions on onPause (like unbinding a service) try to comment it and see if onActivityResult is called (I wasted few good hours on this..)
Thanks to #johndodo (that point to the manifiest) - I find my solution for the same problem.
removing android:noHistory=true at the manifiest" solved this problem for me.

Categories

Resources