Android 13工作配置文件中TileService的requestListeningState崩溃求助
问题分析与解决方案
在Android 13(compileSdk/targetSdk均为33)的工作配置文件环境下调用TileService.requestListeningState()触发IllegalArgumentException: User 10 is not the current user,核心原因是用户上下文不匹配:
工作配置文件对应独立的托管用户(报错中的User 10即为托管用户ID),若调用requestListeningState()时使用的是主用户的Context,系统会检测到当前操作用户与Context所属用户不一致,直接抛出异常。
解决方案
使用工作配置文件专属Context调用
若代码在工作配置文件内的组件(如工作侧Service、Activity)中执行,直接用组件自身的Context即可,不要传递主应用的Context:// 工作侧组件内调用示例 TileService.requestListeningState(this, new ComponentName(this, YourWorkTileService.class));跨用户调用时获取托管用户Context
若需从主用户应用触发工作配置文件的Tile更新,需先获取托管用户的UserHandle,再创建对应Context:UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); // 获取已启用的工作配置文件用户列表 List<UserHandle> workProfiles = userManager.getEnabledProfiles(); if (!workProfiles.isEmpty()) { UserHandle workUser = workProfiles.get(0); // 创建托管用户的Context Context workContext = createContextAsUser(workUser, Context.CONTEXT_IGNORE_SECURITY); // 用工作Context调用更新 TileService.requestListeningState(workContext, new ComponentName(workContext, YourWorkTileService.class)); }注意:跨用户操作需要申请
android.permission.INTERACT_ACROSS_USERS_FULL权限,该权限为系统签名级;若你的应用是工作配置文件专属应用,无需跨用户调用,直接使用第一种方式即可。确认TileService注册正确性
确保工作配置文件应用的AndroidManifest.xml中正确注册TileService,且组件属于工作侧应用:<service android:name=".YourWorkTileService" android:label="@string/work_tile_label" android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"> <intent-filter> <action android:name="android.service.quicksettings.action.QS_TILE" /> </intent-filter> </service>
内容的提问来源于stack exchange,提问作者ThinkDeep
相关产品推荐
相关产品推荐

