Ofir A.
Ofir A.

Reputation: 3162

Execute Android application from native c code

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?

Upvotes: 3

Views: 2168

Answers (2)

Felipe Lavratti
Felipe Lavratti

Reputation: 2967

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.

Upvotes: 0

ehanoc
ehanoc

Reputation: 2217

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.

Upvotes: 1

Related Questions