gpt4 book ai didi

android - 对内容提供者强制执行项目所有权权限

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:13:22 25 4
gpt4 key购买 nike

我有一个在我的几个应用程序之间共享的导出内容提供程序,一旦我发布了 API,第三方就可以免费为其编写他们自己的应用程序。

现在任何客户端应用程序都可以对内容提供程序中的任何项目执行任何 CRUD 操作。我想强制执行一些访问权限,以便应用程序只能修改或删除它创建的项目,同时仍然允许任何应用程序读取提供程序中的所有项目并创建新项目。

有什么方法可以访问调用应用程序的 UID 并将其与新创建的项目一起存储,然后在未来的操作中与该值进行比较?使用包名会更好吗?我假设如果用户卸载并重新安装应用程序,UID 可能会更改,我不希望他们在这样做时失去对这些项目的访问权限。

最佳答案

是的,您可以检查包名。以下是根据调用 uid 检索包名称的方法:

private static Collection<String> getCallingPackages(Context context) {
int callingUid = Binder.getCallingUid();
if (callingUid == 0) {
return Collections.emptyList();
}

String[] packages = context.getPackageManager().getPackagesForUid(callingUid);
return new ArrayList<>(Arrays.asList(packages));
}

这会返回包名称列表,因为引用文档:

In most cases, this will be a single package name, the package that has been assigned that user id.

Where there are multiple packages sharing the same user id through the "sharedUserId" mechanism, all packages with that id will be returned.

关于android - 对内容提供者强制执行项目所有权权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31622184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com