首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android 系统设置,“隐私”中关闭相机权限

Android 系统设置,“隐私”中关闭相机权限

作者头像
小驰行动派
发布2025-07-24 17:41:23
发布2025-07-24 17:41:23
14200
代码可运行
举报
文章被收录于专栏:Android Camera开发Android Camera开发
运行总次数:0
代码可运行

Android系统设置中,“隐私”里面,有一项是 关于“摄像头使用权限”,如果关闭了,则所有的app都没法正常去打开相机。

我们如果需要做类似禁用相机的功能,也是可以参考该部分的代码流程逻辑。

这一功能涉及到的相关代码,如下图所示。

CameraService.cpp

代码语言:javascript
代码运行次数:0
运行
复制
--------- CameraService.cpp
Set camera muting behavior
bool isCameraPrivacyEnabled =
                mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid));
        if (client->supportsCameraMute()) {
            client->setCameraMute(
                    mOverrideCameraMuteMode || isCameraPrivacyEnabled);
        } else if (isCameraPrivacyEnabled) {
            // no camera mute supported, but privacy is on! => disconnect
            ALOGI("Camera mute not supported for package: %s, camera id: %s",
                    String8(client->getPackageName()).string(), cameraId.string());
            // Do not hold mServiceLock while disconnecting clients, but
            // retain the condition blocking other clients from connecting
            // in mServiceLockWrapper if held.
            mServiceLock.unlock();
            // Clear caller identity temporarily so client disconnect PID
            // checks work correctly
            int64_t token = CameraThreadState::clearCallingIdentity();
            // Note AppOp to trigger the "Unblock" dialog
            client->noteAppOp();
            client->disconnect();
            CameraThreadState::restoreCallingIdentity(token);
            // Reacquire mServiceLock
            mServiceLock.lock();
            return STATUS_ERROR_FMT(ERROR_DISABLED,
                    "Camera \"%s\" disabled due to camera mute", cameraId.string());
        }
代码语言:javascript
代码运行次数:0
运行
复制
 bool isCameraPrivacyEnabled =
                mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid));

if (isCameraPrivacyEnabled) {
        // no camera mute supported, but privacy is on! => disconnect
        ALOGI("Camera mute not supported for package: %s, camera id: %s",
                String8(client->getPackageName()).string(), cameraId.string());
bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(userid_t userId) {
    if (!hasCameraPrivacyFeature()) {
        return false;
    }
    return mSpm.isIndividualSensorPrivacyEnabled(userId,
        SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); 
}

----- SensorPrivacyService.java
        isIndividualSensorPrivacyEnabled()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小驰行动派 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档