I know how to do the following
Get results of a PendingIntent via a BroadcastReceiver (e.g. launching a chooser and getting the results after the user makes a choice)
Get results of an IntentSender via an activity's onActivityResult by calling startIntentSenderForResult
However, what I need to do is launch the IntentSender but get the results back via a BroadcastReceiver. Note that IntentSender is given to me, so I have no access to the original PendingIntent corresponding to the IntentSender.
Is there a way to do this, perhaps by bundling additional intents to intentSender.sendIntent()?
I have also read this question and have verified that the onFinished callback in sendIntent() fires right away and doesn't actually return the results of the intent being launched.
Context
I'm doing companion device pairing using Android's CompanionDeviceManager. When it finds a device, it gives me IntentSender chooserLauncher. It expects an activity to call startIntentSenderForResult() with chooserLauncher and get the results back from the activity. However, this does not work for me because I'm developing a library and I don't want the results delivered to the activity, but rather to a BroadcastReceiver that I own.
That is not possible.
Only an Activity can call startActivityForResult() and that will launch an Activity. The result can only be returned to an Activity in onActivityResult(). There is no way to have the result be returned directly to a BroadcastReceiver. It doesn't work that way.
You would have to have the user of your library receive the result in his Activity.onActivityResult() and pass it to your library via whatever means you can provide.
Related
There are already many questions on "How to retrieve the packagename of the caller app" available and most of them refers to the function:
getReferrer()
This works perfectly if the called app isn't in the background, but if it is, then the function will return the Uri of the called app and not the caller.
So my further question is: Is it even possible to guarantee to get the callers packagename? (Assuming that the caller is not spoofing the packagename and calling the app only as usual with startActivity without adding or modifying the intent extras.)
I need to get the Activity from all the other installed apps on the device, possibly the launcher activity, but all of them is ok too. I know this code:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.test");
that gives me an Intent object, but is there an equivalent to get an Activity object? Or some other method?
I've seen many topics similar to this one, but all of them return only the Activity name, the Intent, or a Context.
but is there an equivalent to get an Activity object?
No. Those other apps are in other processes. While those processes may have Activity instances, you cannot access them, because those instances are not in your process.
No.You cannot access Activity instance into other apps.Because both apps considered to be a separate process
you cannot access the context of activity of another apps but if you want to take some result and post some data to other apps then with help of Intent you can do that and get result in onActivityResult() of your activity,s context.
I'm working on a Service which gets a location and I'm using Google Play Services for it.
According to http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#hasResolution%28%29, if hasResolution() returns true, calling startResolutionForResult may resolve an error. But it needs an Activity as first parameter.
Of course ConnectionResult returns a PendingIntent by getResolution() but the Service doesn't have a startIntentSenderForResult() like an Activity.
As far as I know, there is no way to get a result back in a Service.
How can I get a result in the Service? Or is there another proper way?
Edit:
Google Play Service SDK provides GooglePlayServicesUtil.showErrorNotification for background tasks. Of course accepted answer is good solution.
Edit 2:
showErrorNotification is only for a return value of isGooglePlayServicesAvailable.
I would proceed this way:
Show a notification indicating that there is a problem doing task X
(actually, interacting with Google Play Services, but you may say something else
more specific to your app).
For this notification, provide a PendingIntent that starts your activity. As part of the extras for this PendingIntent, pass the PendingIntent provided by ConnectionResult.getResolution(). PendingIntents are parcelable so this shouldn't pose a problem.
In onCreate() for this Activity, obtain the original PendingIntent from the extras, then call startIntentSenderForResult() with it. This will automatically redirect the user to wherever Google Play Services needed him to go (possibly log-in?)
Then, in onActivityResult(), finish the activity, having first notified your service (via an Intent) that the problem is resolved (or not). The transient activity will have been invisible to the user.
I admit this solution is theoretical, but it should work.
I have provide two objects of type PendingIntent to be passed to SmsManager.sendDataSms(). These two pendingIntents are used to trigger a service at a later time.
According to documentation:
this PendingIntent is
broadcast when the message is successfully
sent, or failed. The result code will be
Activity.RESULT_OK
The question is how can I retrieve this 'result code' inside my service?
Basically, except for Activity (through onActivityResult() or something), none of the application components has a mean to retrieve this 'result code' passed to different variations of PendingIntent.send().
First of all, at least in my head, a resultCode for service would not make much sense as it i suppose to be a self contained process. Again, that is just my perspective.
Secondly, the documentation of the getResultCode() for BroadcastReceiver says:
Retrieve the current result code, as set by the previous receiver.
which implies a dependency to an earlier receiver that Services does not have.
As I suggest in the comments to the question, I think the way to go would be to register a BroadcastReceiver inside the Service.
A final note to your comment:
However, I (and I'm sure lots of other people) still wanna know if there's a way of retrieving the result code from inside a service or not. If not, whay such a thing hasn't been explicitely mentioned in documentation?
The fact that the documentation does not state that you can get a resultCode in a Service is a good indication that it is not possible. Furthermore, it is fairly uncommon to document explicitly what some code cannot do.
I think you would retrieve the result code in the BroadcastReceiver and put it in a extra int or something in the intent and send it to the service through the startService.
I am integrating two different APKs whose logic is pretty simple:
App A calls App B passing an amount
App B receives the request
If App B's "active" activity is currently in (main) Activity C
set the amount
allow the user to process the amount
once done processing return a transaction number to App A
Else
return an RESULT_CANCELED to App A
Given this I can certainly can call App B via startActivityForResult, but:
What happens in App B when the (main) Activity C is launched when the application was left sitting in Activity D? How do I detect I was in the middle of Activity D in order to fail and return RESULT_CANCELED?
In normal processing I want to return the resulting transaction number to the caller, I've tried passing an Intent with extras in setResult, but the intent is always returned to App A as null.
Does someone have a simple example of this?
You can only use startActivityForResult() if both the calling activity and the called activity run in the same task. Otherwise startActivityForResult() will generate an immediate call to onActivityResult() with RESULT_CANCELED.
Given the above premise, if App A starts App B using startActivityForResult(), this will always create a new instance of the App B's main activity, and therefore it is not possible for App B to be in any other state.
If the 2 apps are running in different tasks, then you will need to communicate between them using other mechanisms (broadcast intents, background services, shared files, or something similar).