6

I am writing an native application in C++ under android, and I need to broadcast some intents. Is this possible?

If you are going to point me to JNI, please give me more details, as I am not sure how this is done :)


What I will do if this is not possible is having a named pipe between the NDK daemon and a Java-Android-Service. The NDK-daemon will write to the named pipe and then the Java-Android-Service will issue the intent.

Is there a better way?

Charles
  • 50,943
  • 13
  • 104
  • 142
elcuco
  • 8,948
  • 9
  • 47
  • 69

2 Answers2

10

There is a am command that you can run that will send Intents to Activities or Services.

const char *cmd = "am startservice -a %s"
                  " --ei ars_flag 2 --ei invitationType %d"
                  " --ei mode 1 --es ars_gadget_uuid \"%s\""
                  " --ei ars_conn_handle %d"
                  " --es ars_user_uuid \"%s\" --es ars_username \"%s\"";
sprintf (cmdbuffer, cmd, ...);
system (cmdbuffer);
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
SonicBison
  • 790
  • 7
  • 21
  • 1
    adding `--user 0` might also help. E.g. `am startservice --user 0 -a ...` – Tamas Jun 07 '14 at 15:57
  • 1
    @Tamas Thank you so much. I've been battling with security exception and this is the right fix! – Alex Vong Feb 11 '19 at 22:11
  • Does `am` offer anything equivalent to `startActivityForResult()`, i.e. send an Intent to start an activity and then retrieve the result? – user149408 Jun 11 '23 at 22:19
2

You cannot send Intents natively, this is a known limitation of the current NDK (perhaps it will be implemented in the future).

So what you have to do is to use JNI to make an upcall to Java whenever you want to broadcast an Intent (google JNI and upcall if you need to know how this is done), and have Java code that creates and sends the Intent. So if you're starting your application through a Java Activity and calling the native code through JNI, simply implement another method in Java that can receive your upcall. I don't know how much JNI you know, but the wikipedia information should get you started well enough.

Jake
  • 660
  • 1
  • 7
  • 18
  • I know nothing of JNI, besides the theory. Since my application is pure C++, I understand that this is much harder. I assume a full example of creating the JVM and the intents is out of order here...? – elcuco Apr 04 '12 at 14:25
  • 1
    I don't know if it was not possible at that time but it is totally doable at the moment. Future searches should not consider this answer as totally correct. – eozgonul Mar 11 '14 at 12:58
  • 1
    So it is 2020 and sending Intents natively is still not supported? Note: part about using JNI is misleading because Java Runtime is not present in native applications. – user7860670 Jan 14 '20 at 15:43