简体   繁体   中英

Detect if WiFi Scanning always available is on

I'm working on an app, that should detect wifi networks around the device. I want to detect if the "Scanning always available" is switch on or not, but I couldn't find how. I know that it's possible because Google Now, actually does:

在此输入图像描述

You can use the isScanAlwaysAvailable() method in WifiManager .

I just tested this on Android 4.4, and it works.

To query the state, and show the prompt if it's disabled, use this code (I put it in onCreate() ):

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (Build.VERSION.SDK_INT >= 18 ) {
        if (wifiManager.isScanAlwaysAvailable()) {
            Toast.makeText(this, "Scan always available is on", Toast.LENGTH_SHORT).show();
        }
        else{
            startActivityForResult(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE), 100);
        }
    }
    else{
        //Not supported
    }

Then, use this code to capture the decision that the user made in the prompt in the case that it was disabled:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "User enabled Scan always available", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "User did not enable Scan always available", Toast.LENGTH_SHORT).show();
        }
    }
}

Note that you will also need this permission in your AndroidManifest.xml:

  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM