Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 680
Ofir A. Avatar asked Jan 24 '26 05:01

Ofir A.


1 Answers

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.

like image 182
ehanoc Avatar answered Jan 26 '26 21:01

ehanoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!