Android定义了一种权限方案来保护设备上的资源和功能。例如,在默认情况下,应用程序无法访问联系人列表、拨打电话等。下面就以拨打电话为例介绍一下系统对权限的要求。一般在我们的应用中,如果要用到拨打电话的功能,我们会这样编码:
Uri uri = Uri.parse("tel:12345678");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
默认情况下,我们无权访问拨打电话的Activity,控制台将会报以下异常信息:
ERROR/AndroidRuntime: java.lang.SecurityException: Permission Denial:
starting Intent { act=android.intent.action.CALL dat=tel:12345678 cmp=com.android.phone/.OutgoingCallBroadcaster }
......
requires android.permission.CALL_PHONE
看来,我们是缺少了CALL_PHONE这个权限,这个权限是Android系统自带的phone应用里定义的权限:
......
<uses-permission android:name="android.permission.CALL_PHONE" />
......
<activity android:name="OutgoingCallBroadcaster"
android:permission="android.permission.CALL_PHONE"
android:theme="@android:style/Theme.NoDisplay"
android:configChanges="orientation|keyboardHidden">
<!-- CALL action intent filters, for the various ways
of initiating an outgoing call. -->
<intent-filter>
<action android:name="android.intent.action.CALL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CALL" /&g