Kotlin - Internet and location permissions problem

kotlin request permission in fragment
android permissions
onrequestpermissionsresult
android location permission
android internet permission not working
android request permission
read external storage permission android
android camera permission

Ι am writing an android application that uses both location and internet connection permissions and I am running it on my tablet. As you can see, I have included both in the manifest file. The problem is that when OnConnected() function is called the below if in the MainActivity.kt evaluates to true and I can't proceed further in the function. So can you give me some hint to provide some more code if it is not relevant

AndroidManifest.xml :

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

MainActivity.kt

override fun onConnected(p0: Bundle?) {
    if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        return;
    }
//some code 
}

You have to understand the logic behind permissions in Android. Just adding the permissions in the manifest, doesn't mean that the user has authorized the application to do what those permissions enable.

If your if condition evaluates to true, since those permissions are not granted, you have to ask the user for those permissions.

Android has a some good documentation on how to this.

I highly suggest you go over that.

Kotlin, Kotlin - Internet and location permissions problem. android internet permission not working kotlin request permission in fragment android permissions android  Add these permission on top in Manifest <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET"/> and this function should do the trick. It will return boolean based on its connectivity

From Android 6.0 onwards needs to request permission the best way is to do a permissionManager Class to Handle that and then check in your Activity if the user has accepted the permissions:

 class PermissionManager private constructor() {

   fun checkPermission(
    activity: Activity,
    permissionRequests: Array<String>?,
    requestCode: Int
): Boolean {

    permissionRequests?.let {
        it.firstOrNull {
            ContextCompat.checkSelfPermission(
                activity,
                it
            ) != PackageManager.PERMISSION_GRANTED
        }?.apply {
            ActivityCompat.requestPermissions(
                activity,
                permissionRequests,
                requestCode
            )
            return false
        }
    }
    return true
}

companion object {
    val instance by lazy { PermissionManager() }
    const val CAMERA_PERMISSION = 1
}

}

Then in your activity check the permission Result:

   class MainActivity : AppCompatActivity() {
       override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_ar)
    //If the user has the permission accepted the next times
        if (PermissionManager.instance.checkPermission(
            this, arrayOf(
                Manifest.permission.CAMERA
            ), PermissionManager.CAMERA_PERMISSION
        )
        ) {
            initLogic()
        }
    }
    override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    when (requestCode) {
        PermissionManager.CAMERA_PERMISSION -> {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                initLogic()
            } else {
                return
            }
        }
    }
 }
}

Request App Permissions, You declare that your app needs a permission by listing the permission in the app manifest and then For example, an app that needs to access the internet would have this line in the manifest: Display a different button that contains an error icon and color. Android RuntimePermissionsBasic Sample Java | Kotlin. In a number of the example projects created in preceding chapters, changes have been made to the AndroidManifest.xml file to request permission for the app to perform a specific task. In a couple of instances, for example, internet access permission has been requested in order to allow the app to download and display web pages.

you need to ask user for Permission and get the approval see this to request you can request Permission like this

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission. ACCESS_FINE_LOCATION,Manifest.permission. ACCESS_COARSE_LOCATION},
                reqCode);

and you can get the request result like this:

@Override
 public void onRequestPermissionsResult(int requestCode, String 
 permissions[], int[] grantResults) {
                switch (requestCode) {
                    case reqCode: {
        // If request is cancelled, the result arrays are empty.
                        if (grantResults.length > 0
                                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            // permission was granted, yay! Do the
                            // contacts-related task you need to do.          
                        } else {
                            // permission denied, boo! Disable the
                            // functionality that depends on this permission.
                            Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                        }
                        return;
                    }
                    // other 'case' lines to check for other
                    // permissions this app might request
                }
            }

Kotlin - Making Runtime Permission Requests in Android, Android Studio 4.0 Development Essentials - Kotlin Edition Print and eBook In a couple of instances, for example, internet access permission has been requested in get access to experimental features, at the risk of encountering bugs and issues. Location. ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION. You just have to call askPermission with the list of wanted permissions. In Kotlin: askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION){ //all of your permissions have been accepted by the user }.onDeclined { e -> //at least one permission have been declined by the user }

Location Data | Maps SDK for Android, Kotlin. MyLocationDemoActivity: Using the My Location layer, You only need to request one of the Android location permissions, If the permission has not been granted, the Activity is finished with an error message. */ Kotlin Trademark is protected under the Kotlin Foundation. Sponsored and developed by JetBrains. esc

Android Studio 3.4 Development Essentials, Internet. Permission. An attempt to run the application at this point would result in the application failing to launch with an error dialog appearing on the Android  Introduction. Often times we like to track and visualize our applications in a central place. Feeds are great for this! In this tutorial, we'll build an Android app with an activity feed that allows users to broadcast their locations and share with all other connected users in realtime.

Tracking device location for Android | Help, Checking for, requesting, and responding to Android system location permissions​. Checking for and responding to a change in the device's internet connectivity  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Kotlin: How to save image from Internet to internal storage

Comments
  • I have two applications , one that access the location and one that access the internet. In the first one I have the code without the Request you speak about and the location displayed succesfully in the screen when I open the location of the phone. Sorry that I didn't know but the reason is the above.
  • @michalischaviaras - I don't really understand your comment. What do you mean when you say " the reason is the above"? In some Android versions, when downloading an application, it asks you before installing to approve permissions.