Activity 意图权限 Android M SDK 23

Activity Intent Permission Android M SDK 23

从构建工具 22.0 切换到 23.1 后,我在启动 activity 方法时遇到错误。

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

startActivity(callIntent)行显示的错误是

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential 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);
    }

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

What exactly the condition which is required in order to call startActivity method?

您的代码

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

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

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

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>   

但是对于 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);
}