代码:
public class MainActivity extends AppCompatActivity { String Tag = "MainActivity"; final int PERMISSION_REQUEST_CODE = 1; String [] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (Build.VERSION.SDK_INT>Build.VERSION_CODES.O){ if (!hasPermissionsGranted(permission)) { ActivityCompat.requestPermissions(this,permission , PERMISSION_REQUEST_CODE); } } WifiManager wifiManager = getSystemService(WifiManager.class); boolean b = wifiManager.startScan(); Log.e("longjiang", "wifiManager.startScan " + b); for (ScanResult scanResult : wifiManager.getScanResults()) { Log.e("longjiang", scanResult.toString()); } } public boolean hasPermissionsGranted(@NonNull String[] permissions) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { return false; //此时没有授权,返回false } } return true; //已经授权过,返回true } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { Log.d(Tag,"-------onRequestPermissionsResult--------"); if (requestCode == PERMISSION_REQUEST_CODE) { if (!hasPermissionsGranted(permissions)) {//返回false代表申请失败 Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show(); this.finish(); }else { Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show(); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } }
权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
标签:Toast,String,permission,WiFi,扫描,PERMISSION,wifiManager,Android,permissions From: https://www.cnblogs.com/wanglongjiang/p/17651935.html