3

I have a piece of C code (not an NDK app, plain C code), and I am trying to send a broadcast to my app, but all I get is:

Broadcasting: Intent { act=com.example.A_NAME }

I never get the completed message.

The C code I am running is:

    char broadcast[200];
    sprintf(broadcast, "sh %s","com.example.A_NAME");
    FILE* pipe = popen(broadcast, "r");
    char buffer[128];
    char result[1500];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            sprintf(result, "%s%s", result,buffer);
    }
    pclose(pipe);

For a simple bash script, I can get this working. From C, I tried system(), execl() etc, but nothing.

I also tried to put the command in a script file, and execute the script file. If I execute the script from adb shell, it works. If I do it from C code, it does not work.

Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • 1
    Apparently unrelated to your current problem, but to your code: http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – mafso Jul 05 '14 at 00:07

1 Answers1

3

After wasting a day, I got it working:

system("am broadcast -a com.example.DAMN_BROADCAST --user 0");

--user 0, thanks for wasting me a day.

Note: All of the commands that I have tried should work. I 've written just the system command, since it is the easiest one and does the job (as long as you put --user 0). execl, execlp, and pipes should work as well.

Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • I just tried you solution but no matter what is the command in system("...") I always get as ret value of the system call 32512. Any idea? – Giuseppe Mar 07 '15 at 02:49