Android 6.0.1 - Permission issue = wifiManager.get

2019-07-09 06:57发布

问题:

The permissionsList.add() doesn't work but MainActivity.this.requestPermissions() works fine. The issue is that it brings a dialog box asking if the user allow the location permission.

Why adding the permission does not work?

Is there a way to avoid the dialog box?

See my minimal code below:

public class MainActivity extends AppCompatActivity {
WifiManager wifiManager;
WifiBroadcastReceiver broadcastReceiver;

Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = getApplicationContext();

    List<String> permissionsList = new ArrayList<String>();
    permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
            MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
            }
        }

        Button scan = (Button) findViewById(R.id.scan);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);

        scan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               if(wifiManager != null)
                    wifiManager.startScan();
            }
        });


        broadcastReceiver = new WifiBroadcastReceiver();

        // On attache le receiver au scan result
        registerReceiver(broadcastReceiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

    }

    public class WifiBroadcastReceiver extends BroadcastReceiver {

        private WifiManager wifiManager;


        @Override
        public void onReceive(Context context, Intent intent) {
            wifiManager = ((MainActivity) context).getCurrentWifiManager();
            List<ScanResult> listeScan = wifiManager.getScanResults();
          }
    }


    public WifiManager getCurrentWifiManager() {
        return wifiManager;
    }

 }

And here is the manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bernard_zelmans.checksecurity">

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Ping"></activity>
    </application>

</manifest>

回答1:

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

Resource and more to read:

  • https://developer.android.com/training/permissions/requesting.html
  • https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous