How to ask permission to make phone call from Android from Android version Marshmallow onwards?

前端 未结 6 1912
北海茫月
北海茫月 2020-12-31 03:33

I am trying to make a phone call from Android, and I\'ve set run time permissions as well. And it asks whether to allow making phone calls. But when I press allow, the app c

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 03:44

    add new method

    public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
    }
    

    add this in Global

    int PERMISSION_ALL = 1; 
    String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE};
    

    and write below code in onCreate

    if(!hasPermissions(this, PERMISSIONS)){
    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }
    

提交回复
热议问题