2015-10-19 56 views
3

从Build tool 22.0切换到23.1后,我在启动活动方法中收到错误。活动意图权限Android M SDK 23

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

在管线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); 
    } 

状况究竟这是为了调用startActiivty方法所需的条件? 请提供详细信息,如果可能的话,可能会导致相同类型的错误的其他权限。

+1

检查这个演示HTTPS ://github.com/nitiwari-dev/Android-M-RuntimePermissionDemo – nitesh

回答

6

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

您的代码

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); 
} 
+0

谢谢蒂姆。它工作正常。我有一个疑问。在哪里权限将从用户授予应用程序?如果上面的代码写在后台服务中,对话框将如何出现,如下所示https://cloud.githubusercontent.com/assets/10304040/7883031/ee96f3f0-0630-11e5-8b77-44b696bea53a.png –

+0

感谢您的时间:) –