访问 WifiInfo 时在 BroadcastReceiver 中获取 SecurityException
Getting a SecurityException in a BroadcastReceiver while accesing WifiInfo
我在 BroadcastRecevier 中收到 SecurityException:
02-08 12:18:39.169 E/AndroidRuntime(22363): java.lang.RuntimeException: Unable to start receiver surin.de.wakeonlan.BackgroudReceiver: java.lang.SecurityException: WifiService: Neither user 10171 nor current process has android.permission.ACCESS_WIFI_STATE.
02-08 12:18:39.169 E/AndroidRuntime(22363): at surin.de.wakeonlan.BackgroudReceiver.onReceive(BackgroudReceiver.java:26)
我的背景接收器Class:
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //line 26
String ssid = wifiInfo.getSSID();
接收器在清单中的此处注册:
<receiver android:name="surin.de.wakeonlan.BackgroudReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
这也在应用程序标签内的清单中:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
您还需要添加以下权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
并确保 <uses-permission
是清单标签的直接子标签而不是应用程序标签
添加以下 2 个权限(并保留 android.permission.ACCESS_WIFI_STATE
)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
清理然后构建项目
我在 BroadcastRecevier 中收到 SecurityException:
02-08 12:18:39.169 E/AndroidRuntime(22363): java.lang.RuntimeException: Unable to start receiver surin.de.wakeonlan.BackgroudReceiver: java.lang.SecurityException: WifiService: Neither user 10171 nor current process has android.permission.ACCESS_WIFI_STATE.
02-08 12:18:39.169 E/AndroidRuntime(22363): at surin.de.wakeonlan.BackgroudReceiver.onReceive(BackgroudReceiver.java:26)
我的背景接收器Class:
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //line 26
String ssid = wifiInfo.getSSID();
接收器在清单中的此处注册:
<receiver android:name="surin.de.wakeonlan.BackgroudReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
这也在应用程序标签内的清单中:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
您还需要添加以下权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
并确保 <uses-permission
是清单标签的直接子标签而不是应用程序标签
添加以下 2 个权限(并保留 android.permission.ACCESS_WIFI_STATE
)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
清理然后构建项目