5

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().

SztupY
  • 10,291
  • 8
  • 64
  • 87
Joseph_Marzbani
  • 1,796
  • 4
  • 22
  • 36
  • In my answer to http://stackoverflow.com/questions/19083158/send-sms-until-it-is-successful/19084559#19084559 I retrieve the resultcode inside a BroadcastReceiver – cYrixmorten Jan 05 '14 at 13:07
  • getResultCode() was the answer in case of a BroadcastReceiver. But my question still stands: what about the time when the PendingIntent triggers a Service? Is there a way to retrieve the result code inside a Service? – Joseph_Marzbani Jan 06 '14 at 06:53
  • Ok, I might have misunderstood a bit. Why not just register a BroadcastReceiver inside your service to do the job? Should be perfectly valid. – cYrixmorten Jan 06 '14 at 10:20
  • Actually this is what I'm sure I'll end up. 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? – Joseph_Marzbani Jan 06 '14 at 21:21
  • Looks like it's not possible, directly. We have to send it through a `BroadcastReceiver` (or `Activity#createPendingResult()` and `onActivityResult()`, but you probably don't wanna do that outside of testing). `PendingIntentRecord`'s `sendInner()` method handles the dispatch to the relevant component type, and [we can see in the source](https://android.googlesource.com/platform/frameworks/base/+/52a404fa2c54063488c88d1722e8a90ae1cccab3/services/core/java/com/android/server/am/PendingIntentRecord.java#490) that it only sends the (result) `code` parameter for broadcasts and `Activity` results. – Mike M. Feb 05 '23 at 23:04

2 Answers2

1

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.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

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.