我需要我的Android設備上做iwconfig的命令,所以我寫C++代碼和NDK加入它有針對本地方法,這是我的方法:是「iwconfig」命令需要root的android設備嗎?
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
你期待'exec()'返回嗎? (它通常不) – Hasturkun 2013-03-03 11:10:15
是的,它返回字符串值 – Hana90 2013-03-03 14:39:57
iwconfig是舊的,請使用[iw](http://wireless.kernel.org/en/users/Documentation/iw) - 它是Android用戶空間的一部分。 – yegorich 2013-12-24 09:10:16