我需要在我的android设备上发出iwconfig命令,所以我编写了c++代码,并通过ndk将其加入,以获得本地方法,这是我的方法:

jstring Java_com_example_ndk1_MainActivity_exec(JNIEnv* env, jobject javaThis , jstring cmd) {
const char * res;

jboolean isCopy;
res = env->GetStringUTFChars(cmd, &isCopy);

if (isCopy == JNI_TRUE) {
    (env)->ReleaseStringUTFChars(cmd, res);
}

std::string result = exec(res);
return (env)->NewStringUTF((const char* )result.c_str());
}

但在我调用此方法并传递“iwconfig”之后,应用程序停止并引发异常:
03-03 00:07:15.674: E/AndroidRuntime(11872): FATAL EXCEPTION: main
03-03 00:07:15.674: E/AndroidRuntime(11872): java.lang.IllegalStateException: Could not     execute method of the activity
03-03 00:07:15.674: E/AndroidRuntime(11872):    at   android.view.View$1.onClick(View.java:3660)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at   android.view.View.performClick(View.java:4162)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at   android.view.View$PerformClick.run(View.java:17082)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at android.os.Handler.handleCallback(Handler.java:615)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at android.os.Looper.loop(Looper.java:137)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at android.app.ActivityThread.main(ActivityThread.java:4856)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at java.lang.reflect.Method.invoke(Method.java:511)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-03 00:07:15.674: E/AndroidRuntime(11872):    at dalvik.system.NativeStart.main(Native Method)
03-03 00:07:15.674: E/AndroidRuntime(11872): Caused by: java.lang.reflect.InvocationTargetException
 03-03 00:07:15.674: E/AndroidRuntime(11872):   at java.lang.reflect.Method.invokeNative(Native Method)
 03-03 00:07:15.674: E/AndroidRuntime(11872):   at java.lang.reflect.Method.invoke(Method.java:511)
  03-03 00:07:15.674: E/AndroidRuntime(11872):  at android.view.View$1.onClick(View.java:3655)
03-03 00:07:15.674: E/AndroidRuntime(11872):    ... 11 more
03-03 00:07:15.674: E/AndroidRuntime(11872): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: com.example.ndk1.MainActivity.exec:(Ljava/lang/String;)Ljava/lang/String;
 03-03 00:07:15.674: E/AndroidRuntime(11872):   at com.example.ndk1.MainActivity.exec(Native Method)
 03-03 00:07:15.674: E/AndroidRuntime(11872):   at  com.example.ndk1.MainActivity.command(MainActivity.java:34)
 03-03 00:07:15.674: E/AndroidRuntime(11872):   ... 14 more

最佳答案

您看到的错误是“不满意的linkerror”-这意味着您使用JNI做了一些错误的事情。
你编译了你的本地库吗?你能在lib\armeabi\中看到吗?
您是否在java代码中使用“System.load library”加载了本机库?
当然,除此之外,您还假设iwconfig实际上是Android linux中一个有效的二进制/命令,在大多数情况下,恐怕iwconfig并不存在。
关于权限,我认为如果你要在界面列表中查看,那么你将拥有足够的权限。但是如果你想改变一些东西,它不会给你没有根(你见过很多没有你的意愿就可以连接到wifi的应用程序吗?)
您还可以使用Java执行命令,例如。
Runtime.getRuntime().exec(新字符串[]{“ls”,“\tmp”});
因为java代码和本机代码在相同的权限上下文中运行,所以在哪里运行并不重要。希望有帮助。

07-24 09:24