Most Android developers tend to confuse between permission and uses permission while developing applications. The terms may seem similar but in actual usage, they mean a different thing and perform different functions
Apart from simple applications that are mostly for listing static data without requiring the use of external resources, all the other applications that are developed must either use permission or uses permission or both
In this article, we shall focus on what each term means, where each term is used and the differences that occur while using both permission and uses permission in android.
As we all know, android cannot work independently it requires borrowing or what is called importing other features developed by different individuals or cooperates so that it can work
What is permission in android?
Permission in android is a feature that is used to request usage of another application as part of the app you are developing
The permission feature allows the developer to reuse or use features that are already existing in another application without having to redo them again
For example;
- If you want to use the functionality of displaying contacts in your app, you can ask the user to allow the contacts app to be used such that all the users in the phone contacts app can be accessible in the app you are developing
- If you want to use the functionality of maps in your app, you do not have to start from zero yet there is a google maps app that you can ask for its permission and once it is granted your app can easily access and use the features provided by google maps
For permission to work in android, it must have the input of the user interacting with the app. The developer adds the permission of the feature that wants to use or import, but the user must allow the permission else it will not work
The image below shows an example of an app requesting the user to accept or allow it to access the location
If the user denies, the permission will not work
How to request permission in android
To request for a permission in android, you have to put the code that requests for a specific permission in the file that you want to enable the permission
The code snippet below shows how to request a permission using permission in android
if (ContextCompat.checkSelfPermission([email protected],
Manifest.permission.ACCESS_FINE_LOCATION) !==
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale([email protected],
Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions([email protected],
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
} else {
ActivityCompat.requestPermissions([email protected],
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
}
}
What is uses-permission in android?
Uses-permission is a feature added in android when you want to add a permission that your app has. For example, accessing the internet in your app is not always defined by default, therefore to connect to the internet, you must add that permission using the uses-permission feature
The good thing with uses-permission in android, it does not require the input of the user unlike the permission when added
The uses-permission when used, automatically adds the permission and makes it usable instantly provided the right procedures are followed
The uses-permission is used globally in the app not just in one file therefore it is defined in the manifest file
To add the internet permission using the uses-permission in android, we add the code shown below in the manifest file
<uses-permission android:name="android.permission.INTERNET"/>
A sample full manifest file code is as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.checkinternet">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CheckInternet">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Conclusion
In the above discussion, we have shown the differences between permission and uses-permission in android whereby we have noted that a permission is used to request access to use other application features while uses-permission imports features that are not defined in android but are needed to be used example internet access
The permission needs to use other applications features but uses-permission needs to use a specific feature
Mostly, you will note that both permission and uses-permission are used together in most cases they have features that are interdependent on each other
The permission needs the input of the user while uses-permission once defined does not need the user to contribute
The permission is defined on a particular file since it is used where it is required but the uses-permission is used globally therefore it is defined in the manifest file