Android声明和使用权限

Android系统通过权限机制保护用户资源,应用需声明权限才能访问特定功能,如拨打电话。本文通过示例介绍如何在应用中定义和使用拨打电话的权限,包括权限类型和权限组的解释,并展示了跨应用访问权限的场景。
摘要由CSDN通过智能技术生成

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
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值