活动意图权限Android M SDK 23

Mad*_*bar 3 android android-intent android-permissions

从Build工具22.0切换到23.1后,我在启动活动方法中遇到错误.

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
Run Code Online (Sandbox Code Playgroud)

显示的错误startActivity(callIntent)

调用需要用户可能拒绝的权限:代码应明确检查权限是否可用(带 checkPermission)或显式处理潜力 SecurityException

位置和内容解析程序显示相同的错误.我通过检查条件来解决它

    if (mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                            || mContext.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,
                   MIN_TIME_BW_UPDATES,
                   MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    location =  LocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
Run Code Online (Sandbox Code Playgroud)

调用startActiivty方法需要什么条件?如果可能,请提供可能导致相同类型错误的其他权限的详细信息.

Tim*_*Tim 6

调用startActivity方法需要什么条件?

你的代码

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
Run Code Online (Sandbox Code Playgroud)

使用Intent.ACTION_CALL需要许可的 意图,即权限android.permission.CALL_PHONE.

通常你会把它放在你的清单中

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>   
Run Code Online (Sandbox Code Playgroud)

但是使用api 23+,您必须检查权限运行时,与您对位置的操作相同:

if (mContext.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + phoneNumber));
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(callIntent);
}
Run Code Online (Sandbox Code Playgroud)