2

I am trying to launch an android applications from native code.

In adb shell we can launch applications using "am" command.

Link:

How to run (not only install) an android application using .apk file?

Is there any way to invoke this "am" command through C code?

I tried the following line but exec is returning -1:

  ret = execl("/system/bin/am", "start", "-a", "android.intent.action.MAIN",
 "-n", "com.android.settings/.Settings", (char *)NULL);

Is this right or not?

Community
  • 1
  • 1
Bineesh
  • 31
  • 1
  • 6

2 Answers2

1

I got the answer... I 'exec'ed the shell itself and gave it the command... It worked

ret = execl("/system/bin/sh", "sh", "-c", "am start -a android.intent.action.MAIN -n   com.android.browser/.BrowserActivity", (char *)NULL);

Thanks to m0skit0, Padma Kumar, Yuri ...

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
Bineesh
  • 31
  • 1
  • 6
  • It's not entirely clear why using a shell would make a difference here. However note that calling an exec family function without first calling fork() **replaces** the calling program - which, if that was an android app will look like a crash. – Chris Stratton Jun 21 '13 at 12:25
0

you should use system() family method ,first it will fork child process and invoke shell then return,so it will not block android main thread

Dartan Li
  • 217
  • 2
  • 5
  • This should maybe have been a comment, not an answer. With a bit more rep, [you will be able to post comments](http://stackoverflow.com/privileges/comment). – Nathan Tuggy Feb 09 '15 at 02:58
  • system will block whatever thread it is called from. So don't call this from your main thread. – vharron Apr 30 '19 at 03:30