Android获取WIFI 的ssid 方法适配Android9.0

Android获取WIFI 的ssid 方法适配Android9.0

demo传送门:https://download.csdn.net/download/godnessismymine/10803572

 

首先在Androidmanifast 中添加权限

 
 
## 获取SSID 的方法如下

/**
     * 获取SSID
     * @param activity 上下文
     * @return  WIFI 的SSID
     */
    public String getWIFISSID(Activity activity) {
         String ssid="unknown id";

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O||Build.VERSION.SDK_INT==Build.VERSION_CODES.P) {

            WifiManager mWifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

            assert mWifiManager != null;
            WifiInfo info = mWifiManager.getConnectionInfo();

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return info.getSSID();
            } else {
                return info.getSSID().replace("\"", "");
            }
        } else if (Build.VERSION.SDK_INT==Build.VERSION_CODES.O_MR1){

            ConnectivityManager connManager = (ConnectivityManager) activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connManager != null;
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected()) {
                if (networkInfo.getExtraInfo()!=null){
                    return networkInfo.getExtraInfo().replace("\"","");
                }
            }
        }
        return ssid;
    }

Mainactivity代码:

public class MainActivity extends AppCompatActivity {
    private PermissionHelper permissionHelper;

    TextView mTvSSID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
         mTvSSID = findViewById(R.id.tvSSID);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        permissionHelper = new PermissionHelper(this);
        getWifiSSid();
    }

    private void getWifiSSid() {
        permissionHelper.check(Manifest.permission.ACCESS_FINE_LOCATION).onSuccess(this::onSuccess).onDenied(this::onDenied).onNeverAskAgain(this::onNeverAskAgain).run();
    }
    private void onSuccess() {

        mTvSSID.setText(DeviceUtil.INSTANCE.getWIFISSID(this));
    }

    private void onDenied() {
        ToastUtil.INSTANCE.show(this, "权限被拒绝,9.0系统无法获取SSID");
    }

    private void onNeverAskAgain() {
        ToastUtil.INSTANCE.show(this, "权限被拒绝,9.0系统无法获取SSID,下次不会在询问了");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

Android8.1获取方法

需要在Androidmanifast.xml文件中添加如下权限

 
  
ConnectivityManager connManager = (ConnectivityManager) activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connManager != null;
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected()) {
                if (networkInfo.getExtraInfo()!=null){
                    return networkInfo.getExtraInfo().replace("\"","");
                }
            }

9.0 以后需要申请运行时权限



 

权限申请框架

在model 中的gradle中加入以下依赖。

 implementation 'ru.alexbykov:nopermission:1.1.2'

 

你可能感兴趣的:(安卓bug解决)