是否可以在后台服务中获取wifi ssid?
Is it possible to get the wifi ssid in the background service?
我正在尝试在后台获取 wifi ssid。
虽然它在前台,但效果很好。
但是当它进入后台时,在我的前台服务中,它 returns .
当然是获取Location权限了。
是否可以在该服务中获取 ssid?
ConnectivityManager connManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("checkWifi", "checkWifi" + wifiInfo.getSSID());
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
需要 ACCESS_BACKGROUND_LOCATION
权限 Android 10+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
谢谢@CommonsWare!
我正在尝试在后台获取 wifi ssid。
虽然它在前台,但效果很好。
但是当它进入后台时,在我的前台服务中,它 returns .
当然是获取Location权限了。
是否可以在该服务中获取 ssid?
ConnectivityManager connManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("checkWifi", "checkWifi" + wifiInfo.getSSID());
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
需要 ACCESS_BACKGROUND_LOCATION
权限 Android 10+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
谢谢@CommonsWare!