我需要我的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
2013-03-02
Hana90
+0
你期待'exec()'返回吗? (它通常不) –
2013-03-03 11:10:15
+0
是的,它返回字符串值 –
2013-03-03 14:39:57
+0
iwconfig是旧的,请使用[iw](http://wireless.kernel.org/en/users/Documentation/iw) - 它是Android用户空间的一部分。 –
2013-12-24 09:10:16