3

Continue my previous question, I succeeded to execute a process from an Android application. Now I can see two process like I wanted, they both up and running. Now I need to execute an Android application from the native C code.

Found similar questions (1, 2), but the following solutions did NOT work for me.

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

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

None of the lines above didn't executed anything.

Even executing execl command with fork as follows did NOT help.

if (!fork()) {
    execl...
}

Can you please give me some kind of a clue? Thanks.

UPDATE: I've manage to print the stdout to the Android log, I'm getting errno

"Exec format error"

message from the execl method. Anybody have an idea how I can resolve this?

20
  • Why do you need to call apps from native C? You could try calling from your native code a java method that launches your desired app (by creating an intent). Commented Apr 3, 2014 at 12:43
  • @Tonoicutzu Yes, you are right, this is kind of weird stuff. What I want is to execute the browser when the user uninstall my application. The only way I know is to execute native C code in a new process from my application, and from there to execute the browser. Because my application is no longer running I can't call java method, but only to use the native code. Please read my previous question, you can find more details there. Thanks. Commented Apr 3, 2014 at 12:51
  • 2
    "am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity" It is the command from the first example you provided in the question. Since you verified that this command works fine when run from a terminal, now all we need to do is learn how to exec a command properly from within C code. Commented Apr 6, 2014 at 11:43
  • 1
    As I said before, you should use the ndk build system, which you are not. Using its compiler directly is not recommended. Put a hello world printf in your code so you can tell if it even starts, and make sure the execute bit is set. Commented Apr 6, 2014 at 18:29
  • 1
    Why don't you guys sum up all this comment discussion into a readable answer below ? Commented Apr 9, 2014 at 2:02

2 Answers 2

1

Maybe you can write this from the Android/ Java / VM scope and call it from native code using NDK & JNI (Java Native Interface) ?

Example :

From your app:

class MyActivity extends Activity {

     public native int nativeMethodName();

     public void launchSomeAppMethod() {
         // launch some app
         Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.someapp");
         startActivity(LaunchIntent);
     }
 }

Native :

jint Java_com_yourpackage_appname_MyActivity_nativeMethodName(JNIEnv* env, jobject thiz) {
            //....
            // do your native work here
            // ...

            // call your obj instance that can launch another app
            jclass cls = (*env)->GetObjectClass(env, thiz);
            jmethodID method = (*env)->GetMethodID(env, cls, "launchSomeAppMethod", "()V");
            (*env)->CallVoidMethod(env, thiz, method);
}

Hope it helps dude.

Sign up to request clarification or add additional context in comments.

Comments

0

The error printed smells like permission problem.

Are you trying to run the application from an SD card? SD Cards by default are mounted without execute permission, you need to remount it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.