0

My app uses Internet access to display ads so I have the Internet permission declared in Manifest. With latest Android releases, the user can disable this permission. My problem is that if the user cancels this permission, the app is not able to load ads. I know I can check if there is any current connection, in that case I will not try to load the app. But I want to know if the user has denied that permission in Android settings. The idea is to prompt the user to enable it or the app will not run.

I add a screenshot of the Huawei Android Pie where the user can modify these settings

Ton
  • 9,235
  • 15
  • 59
  • 103
  • 1
    user can not deny internet permission, you just have to declare it in `Manifest` only not need to do any permission handling for his – Priyanka Aug 08 '19 at 08:29
  • There is no need to ask permission for the internet to the user. Just add it in manifest. – Jignesh Mayani Aug 08 '19 at 08:31
  • Yes, the user can enter in Settings, Apps, MyApp, Data use, disable Internet. This in a Huawei with Android Pie – Ton Aug 08 '19 at 08:33

2 Answers2

0

You just need to check first that whether the user enable (Allow the permission) or not. If not then redirect the user to the phone setting so that the user allow that permission otherwise just finish the app.

if (ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED )
        {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,  Manifest.permission.CAMERA)) {
                //  if user blocked permission
                android.app.AlertDialog.Builder dlgAlert  = new android.app.AlertDialog.Builder(this);
                dlgAlert.setPositiveButton("Take me to app settings",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                    }
                });
                dlgAlert.setNegativeButton("Cancel",null);
                dlgAlert.setCancelable(true);
                dlgAlert.setIcon(R.drawable.appicon);
                dlgAlert.setMessage("The app needs the permission please allow it other wise you cannot proceed.");
                dlgAlert.setTitle("Oops!");
                dlgAlert.create().show();

            }

        }
    else
    {
    finish();
    }
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Adam Noor
  • 101
  • 3
  • sorry i don't know how to create an answer i mean code in answer but this is the code for checking the permissions. – Adam Noor Aug 08 '19 at 08:37
  • I will try that. Thanks – Ton Aug 08 '19 at 08:38
  • again, checking permissions has nothing to do with the question – Tim Aug 08 '19 at 09:14
  • @TimCastelijns friend the question is that if the user did not connect with the internet then the developer don't want that the app runs so by checking permission you can finish i mean close the app if the internet is not connected. – Adam Noor Aug 08 '19 at 10:18
  • @TimCastelijns Otherwise if you have any other solution then tell him – Adam Noor Aug 08 '19 at 10:18
  • @Ton if you want to trigger that whenever the app is connected or disconnected by internet then you have to add this in the minifest file And create a class and extend it by BroadCastReciever and in on Receive Method you will get know whenever the phone is connected with internet or disconnect. – Adam Noor Aug 08 '19 at 10:21
  • Everything you said is wrong. He does not want what you think he wants. – Tim Aug 08 '19 at 11:41
  • @TimCastelijns maybe i didn't understand the question so if you know the right answer then give him – Adam Noor Aug 08 '19 at 12:56
  • I don't have to post a different answer just to point out that your answer is wrong – Tim Aug 08 '19 at 13:01
-1

If internet connection is all you want to check, you dont have to check permissions for this because Internet is not a critical permission. What you can simply do is use ConnectivityManager class to check if the device has active internet connection or not and then write your logic accordingly.

Refer: This on how you can check for internet connectivity.

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • I don't need to know if the app is able to connect right now. I need to know if the app could be able to access whenever the device is connected – Ton Aug 08 '19 at 09:36