Android 10: How to detect if the user accepted/rejected the permission CHANGE_WIFI_STATE from the Android system notification?

501 Views Asked by At

I'm using addNetworkSuggestions on Android 10 to auto connect to an existing Wifi access point using this doc reference.

I'm using the following code :

val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;

val status = wifiManager.addNetworkSuggestions(suggestionsList);
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
    // do error handling here
}

// Optional (Wait for post connection broadcast to one of your suggestions)val intentFilter = IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);

val broadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (!intent.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            return;
        }
        // do post connect processing here
    }
};
context.registerReceiver(broadcastReceiver, intentFilter);

I'm using an Android 10 device and I'm getting a notification inviting the user to allow or not the permission:

Android permission notification

According to the doc, this is the correct behaviour on Android 10:

On Android 11 (API level 30) and higher, the user sees a dialog if the app is running in the foreground, and a notification if the app is running in the background.

On Android 10 (API level 29), the user sees a notification, regardless of whether the app is running in the foreground or the background.

If the permission is not yet accepted, I want to show some text inviting the user to accept the permission notification. But if the user has already accepted the permission notification, I would hide this text.

The question is: How to detect if the user accepted/rejected the permission in the Android system notification ?

I've tried the following:

  • I've checked the result of wifiManager.addNetworkSuggestions method but it returns STATUS_NETWORK_SUGGESTIONS_SUCCESS even though the permission was not granted yet.
val status = wifiManager.addNetworkSuggestions(suggestionsList);
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
    // this is not triggered even though the user didn't accept the permission notification
}
  • ActivityCompat.checkSelfPermission
ActivityCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE)

But it always returns true

  • AppOpsManager.unsafeCheckOp

From this issue:

appOpsManager.unsafeCheckOp("android:change_wifi_state", Process.myUid(), applicationContext.packageName) == AppOpsManager.MODE_ALLOWED

It returns true even if the permission was not yet accepted/rejected. But it returns false when the user has rejected the permission. This works only for the case when the user has refused the Android permission notification.

  • From Desmond's suggestions I've tried using requestPermissions on Manifest.permission.CHANGE_WIFI_STATE. I correctly receive the PackageManager.PERMISSION_GRANTED value. But I keep getting the notification permission.

I have the following permissions in the manifest:

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

There are 0 best solutions below