krv
krv

Reputation: 2940

Android permissions for API 23 and up

until now I have been adding the necessary permissions for the app in the AndroidManifest.xml file as follows

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />

As I have read on the web. For API 23 and up the app needs to ask for permissions on the fly, as and when they are needed.

So my question is that, do I still need to add permissions in the AndroidManifest.xml file or just handle it on the fly or do I have to do Both.

-Thanks

Upvotes: 0

Views: 3973

Answers (4)

Nick Iliev
Nick Iliev

Reputation: 9670

You can ease your life and use nativescript-permissions to ask for permission runtime (less code and won't need to go through native implementations) You will still have to ask for those permissions in Androidmanifest.xml.

Example for runtime permissions with nativescript-ermissions

var permissions = require("nativescript-permissions");

permissions.requestPermission([
    "android.permission.INTERNET",
    "android.permission.READ_EXTERNAL_STORAGE",
    "android.permission.WRITE_EXTERNAL_STORAGE",
    "android.permission.CALL_PHONE",
    "android.permission.SEND_SMS"
], "I need these permissions")
    .then(function (res) {
        console.log("Permissions granted!");
    })
    .catch(function () {
        console.log("No permissions - plan B time!");
    });

Examples for AndroidManfiest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/> 
<uses-permission android:name="android.permission.SEND_SMS"/>

Full example implemented in this app

Upvotes: 4

Hitesh Danidhariya
Hitesh Danidhariya

Reputation: 759

If you got time there are two types of permission.

Dangerous are to asked on runtime.

For more details read here.

Upvotes: 1

A.Arjun
A.Arjun

Reputation: 105

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. use below code to check permissions

 private void checkPermission() {
   // give whatever permission you want. for example i am taking--Manifest.permission.READ_PHONE_STATE  

    if ((Build.VERSION.SDK_INT >= 23) &&(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED ) ){

        requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 2);
      onRequestPermissionsResult(int, String[], int[]) overriden method
    }else {
          //write your code here. if permission already granted


    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 2) {
        Log.i("resultcode",""+requestCode);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.i("resultcode",""+requestCode);


        }
        else {
            Toast.makeText(getApplicationContext(),  "Permission Denied", Toast.LENGTH_SHORT).show();
        }
    }
}

Upvotes: 0

SANJAY GUPTA
SANJAY GUPTA

Reputation: 1604

You need to do both things. You have to add it in menifest and manage it also.

Upvotes: 6

Related Questions