1、AndroidManifest.xml 配置权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
2、手动申请权限
在登录或首页activity中添加该方法,并在onCreate()方法中调用:
protected void applyPermission() {
String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.MODIFY_AUDIO_SETTINGS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_SETTINGS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.CHANGE_NETWORK_STATE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_CALENDAR,
Manifest.permission.READ_CONTACTS,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WAKE_LOCK,
Manifest.permission.CAMERA,
Manifest.permission.MODIFY_PHONE_STATE,
Manifest.permission.CHANGE_WIFI_MULTICAST_STATE
};
ArrayList<String> toApplyList = new ArrayList<>();
for (String perm : permissions) {
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
toApplyList.add(perm);
//进入到这里代表没有权限.
}
}
String[] tmpList = new String[toApplyList.size()];
if (!toApplyList.isEmpty()) {
ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 1);
}
}
3、获取当前连接的wifi名称的方法
private void getConnectWifiSsid2(){ WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); //这里获取的是连接的wifi名称 String connect_wifi_name = mWifiManager.getConnectionInfo().getSSID().replace("\"", ""); Log.d("getConnectWifiSsid", "connect_wifi_name:" + connect_wifi_name); //打印全部wifi信息 Log.d("getConnectWifiSsid", "SSID:" + connect_wifi_name); //打印SSID }
提示:APP安装完成之后,重启生效