TYZRPVX
TYZRPVX

Reputation: 90

How to check if permission has been used in Android?

I declared some permissions in an Android app, after I modified the code, I am not sure if some permissions have been used. So how to check if one permission has been used in an Android app.

Upvotes: 3

Views: 1737

Answers (3)

BigDru
BigDru

Reputation: 161

Android Studio will fail build if you don't have a required permission declared in manifest.

Easiest way is to check if permission is being used in your modified code is to remove said permission from the manifest and see if it still builds.

Upvotes: 4

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.

A permission is a restriction limiting access to a part of the code or to data on the device. The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience.

  1. App Manifest

  2. Is there a way to check for manifest permission from code?

You should use

PackageManager

Android PackageManager class is used to retrieve information on the application packages that are currently installed on the device. You can get an instance of PackageManager class by calling getPackageManager(). PackageManager provides methods for querying and manipulating installed packages and related permissions, etc. In this Android example, we we get list of installed apps in Android.

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

Courtesy

Upvotes: 2

Vinay Pandravada
Vinay Pandravada

Reputation: 88

Try this:

PackageManager pm = appcontext.getPackageManager();
int hasPermission = pm.checkPermission(
     android.Manifest.permission.INTERNET, 
    appcontext.getPackageName());
if (hasPermission != PackageManager.PERMISSION_GRANTED) {
   // do stuff
}

Upvotes: 2

Related Questions