Get Back Result from Android native Calculator - android

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.

Related

How to distinguish what cancelled Game Services sign in?

Using GameHelper in my game, I paid attention that onActivityResult() if I get Activity.RESULT_CANCELED - the assumption is that user cancelled the sign in. In the other hand, according to Android documentation:
If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.
For me, it seems that in some old devices, I get RESULT_CANCELED as a result of some crash on low connectivity, and it makes me some issues.
The question is - can be RESULT_CANCELED as a result of crash, and if it can, how can i differ between user cancelled and crash?
Thanks in advance
You can use requestCode parameter of onActivityResult method :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == GOOGLE_PLUS_LOGIN_REQ) {
// TODO : implement your logic
}
}
You need to learn value of requestCode by trying out sign in cancel scenarios or looking into GameHelper.java source code. After that you can set this value to a static final GOOGLE_PLUS_LOGIN_REQ variable and use as stated above.

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

action code for taking pictures

im trying to take a picture inside an android app, and im trying to use the android devloper tutorial:
http://developer.android.com/training/camera/photobasics.html
they bring the following code:
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
i can't understand what is this action code and what it should be for taking pictures
thanks!
Per Getting a Result from an Activity, the second parameter to startActivityForResult is used to distinguish between multiple different requests (say, if you got results from both the camera and the gallery you'd want to know where the result is from).
That same actionCode is then returned as the requestCode in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
The point is that it doesn't matter exactly what the number is (0, 1, 100, 5439, whatever), only that it is unique within your Activity. Therefore if you are only calling startActivityForResult in one place for one result, any number will do (as there is nothing to conflict with)
Intents are designed to allow your application to interact with others. In this case, your application will bring up the camera app, and the result will be sent back into your app.
A great place to get started with understanding this is, is the Android training "Interacting with Other Apps".

Android Facebook SDK 3.0 auth

I'm actually following SessionLoginFragment.java example from facebook sdk samples.
What I really don't understand is this:
when I make
session.openForRead(new Session.OpenRequest(fragment).setCallback(statusCallback));
to log my user to facebook and ask basic read permission (just to test the integration) it simply does not work.
I digged a bit with the debugger and I followed the path. If you don't put a requestCode to the OpenRequest of the session it will give it a random one (and this is ok).
openForRead (my actual session is in CREATED status) will create the permission dialog. When you hit the "Ok" button it will perform a
request.getStartActivityDelegate().startActivityForResult(intent, request.getRequestCode());
You can see the code from the fb sdk source code. Well the requestCode is the same of the session (and here's ok).
When fb log you into the system it will finish his facebook.LoginActivity and call me back my onActivityResult in my activity. The problem is that here the requestCode is different from the request's one. And I don't know why and where it comes from!
If I go into my fb account my application is there, so it means that I've done the correct auth flow that finish well. But I wont get correctly authenticated from my app because of this problem.
Do you know why and how can I solve it?
Thanks.
UPDATE WITH FLOW DETAIL:
This is the actual flow (from fragment):
session.openForRead(new Session.OpenRequest(fragment).setCallback(statusCallback));
After creating it, the request code is (always) 64206
Now openForRead flow will call (final part)
request.getStartActivityDelegate().startActivityForResult(intent, request.getRequestCode());
That call the LoginActivity from facebook SDK and do the client/server validation/oauth
Now on my activity is called onActivityResult (not on the fragment but on the activity part)
and here I call
Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);
And here the requestCode is requestCode 129742
How is it possible? As I already said, the problem in all this flow is that requestCode returned to onActivityResult is different from my pendingRequest requestCode and this break (getActiveSession().onActivityResult return without executing code) the login client part.
I ran into the same problem, except that in my case the offending requestCode was 326350 (0x4face). And I was indeed calling super.onActivityResult, so the workaround proposed by Eric Savage was already in place but not effective. Weirdest thing of all, this stuff did work just a couple of weeks ago, and the offending behaviour appeared without myself upgrading anything (Facebook SDK version, Android version, support library version, even the phone on which I'm developing/testing, are all the same as when I had it working).
However, Eric's answer contains other interesting hints, which I exploited to make my code work again. Basically, instead of passing the whole requestCode to Session.onActivityResult, I cut the lowest 16 bits and pass those only.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session session = Session.getActiveSession();
int sanitizedRequestCode = requestCode % 0x10000;
session.onActivityResult(this, sanitizedRequestCode, resultCode, data);
}
I do believe this is a bug that should be fixed in the Facebook SDK, and would insist to have it patched for the next release.
Just ran into this same issue. The difference between 64206 (0xface) and 129742 (0x1face) is because FragmentActivity has tacked on an extra 0x10000 to determine which fragment it came from. This was resolved by making sure the super activity was called during onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// ...
}
}
You "Can only use lower 16 bits for requestCode", as stated in FragmentActivity.startActivityFromFragment

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