5

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,010
  • 13
  • 100
  • 141
elcuco
  • 8,684
  • 9
  • 45
  • 67

2 Answers2

9

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'
  • 98,216
  • 36
  • 202
  • 244
SonicBison
  • 780
  • 7
  • 20
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
  • 630
  • 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