protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
List<ScanResult> wifiScanList = mainWifiObj.getScanResults(); //error in this line
String data = wifiScanList.get(0).toString();
我正在尝试扫描 wifi 并想要特定的接入点信号强度。所以,我从网站上复制了这段代码:http ://www.tutorialspoint.com/android/android_wi_fi.htm
当我尝试运行此代码时,我的应用程序崩溃了。除了这个,我什么都没做。如果我评论最后两行,我的应用程序不会崩溃,甚至不会给我任何输出。
谁能告诉我,我该如何阻止这个错误。错误:App_name 强制关闭。
Log Cat 中的错误是:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifisignalstrength/com.example.wifisignalstrength.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10112 nor current process has android.permission.ACCESS_WIFI_STATE.
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
android.app.ActivityThread.access$600(ActivityThread.java:127)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(24463): at android.app.ActivityThread.main(ActivityThread.java:4511)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24463): Caused by: java.lang.SecurityException: WifiService: Neither user 10112 nor current process has android.permission.ACCESS_WIFI_STATE.
E/AndroidRuntime(24463): at android.os.Parcel.readException(Parcel.java:1327)
android.os.Parcel.readException(Parcel.java:1281)
android.net.wifi.IWifiManager$Stub$Proxy.getScanResults(IWifiManager.java:688)
android.net.wifi.WifiManager.getScanResults(WifiManager.java:804)
com.example.wifisignalstrength.MainActivity.onCreate(MainActivity.java:47)
android.app.Activity.performCreate(Activity.java:4486)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
... 11 more