1

My problem is the request permission window doesn't show up for the permissions below. But if I changed the permissions to request from

static final String[] PERMISSIONS = new String[]{
    Manifest.permission.PROCESS_OUTGOING_CALLS,
    Manifest.permission.CALL_PHONE
};

to

static final String[] PERMISSIONS = new String[]{
    Manifest.permission.READ_EXTERNAL_STORAGE
};

it works and I don't know why. So the dialog shows up for READ_EXTERNAL_STORAGE, but not for PROCESS_OUTGOING_CALLS and CALL_PHONE. Is my code wrong?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication">
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>


Activity:

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.support.v4.content.PermissionChecker.PERMISSION_GRANTED;

public class MainActivity extends AppCompatActivity {
    protected static final int PERMISSION_REQUEST_CODE = 11186;
    static final String[] PERMISSIONS = new String[]{
        Manifest.permission.PROCESS_OUTGOING_CALLS,
        Manifest.permission.CALL_PHONE
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission(PERMISSIONS);
    }
    protected void checkPermission(String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] requestPermissions = getRequestPermissions(permissions);
            if (requestPermissions.length > 0)
                requestPermissions(requestPermissions, PERMISSION_REQUEST_CODE);
        } else {
            int[] grantResults = new int[permissions.length];
            Arrays.fill(grantResults, PERMISSION_GRANTED);
            onRequestPermissionsResult(PERMISSION_REQUEST_CODE, permissions, grantResults);
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private String[] getRequestPermissions(String[] permissions) {
        List<String> list = new ArrayList<>();
        for (String permission : permissions) {
            if (checkSelfPermission(permission) == PERMISSION_DENIED) {
                list.add(permission);
            }
        }
        return list.toArray(new String[list.size()]);
    }
}

build.gradle:

  android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.administrator.myapplication"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

Thanks for your help!

Encore
  • 11
  • 1
  • 3

3 Answers3

0

Please check your minSdk version in your build.gradle file, and make sure your APK is deploying in SDK(mobile) version above 22 (M).

Paru
  • 57
  • 1
  • 7
  • minSdkVersion 17 targetSdkVersion 25 is that wrong? – Encore May 07 '17 at 06:40
  • Its not wrong , check your mobile version is M or higher , and if u want to test it manually goto settings-> apps->select your application-> u will get the options to turn off permission manually. then u can check it. hope it will helps. else dubug your activity – Paru May 07 '17 at 06:46
  • my mobile is android 7.0.and I check setting too.My app don't have the call_photo permission ,but it don't show the request whidow pops ,when i debug ,code can run to "requestPermissions(requestPermissions, PERMISSION_REQUEST_CODE);" this line,but nothing show TnT – Encore May 07 '17 at 06:51
  • but I run in android7.0device – Encore May 07 '17 at 06:58
0

It has nothing to do with min sdk. Target Sdk must be 23 or higher to ask permissions and also runtime permission dialog is available for devices with 23 or above. If you try your code on a device with lower than 23 you won't be able to see permissions dialog, it will be like those permissions are accepted on the background.

Another issue is permissions is there are 2 types of permissions:

Normal permissions, and dangerous permission.

Only dangerous permissons can be asked on runtime. But it should work for Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.CALL_PHONE since they are in dangerous permissions group. You can check dangerous and normal permissions.

Community
  • 1
  • 1
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • thanks,but the problem cant still be solved ,why Manifest.permission.READ_EXTERNAL_STORAGE work , Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.CALL_PHONE dont work,the are also belong to dangerous – Encore May 07 '17 at 07:07
  • I actually never tried those 2 permissions but i think issue can be about the code. There could be something wrong with asking multiple permissions. Can you check it with another permissions such as location or camera? – Thracian May 07 '17 at 07:13
  • I have tried some.dont work:SEND_SMS,location,GET_ACCOUNTS work :camera,READ_CALENDAR – Encore May 07 '17 at 07:35
  • Use [this utility class](https://stackoverflow.com/questions/42143285/checking-if-permissions-have-been-granted-already-by-user-in-android/42143563#42143563) to ask permissions one by one, or multiple permissions at once. Modify location, camera, etc. to your needs and it will be fine and your MainActivity will be cleaner. – Thracian May 07 '17 at 07:38
  • I think your issue is `ActivityCompat.requestPermissions(activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_PERMISSION_MULTIPLE)` – Thracian May 07 '17 at 07:41
  • I try it too TnT~~~could you please try to run the code on your free time ,please~~ – Encore May 07 '17 at 07:45
0

I faced the same problem. Maybe someone will be useful. You need to add to the Manifest:

<uses-feature android:name="android.hardware.telephony" android:required="false"/>