从我自己的应用程序查询我自己的ContentProvider时获得权限拒绝 [英] Getting a Permission Denial when querying my own ContentProvider from my own App

查看:638
本文介绍了从我自己的应用程序查询我自己的ContentProvider时获得权限拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,我正在使用ContentProvider来保存和还原信息.主应用程序以及几个服务都使用此ContentProvider,但是所有这些服务都位于同一apk中,并且所有服务都位于默认(主)进程中.

In one of my apps, I'm using a ContentProvider to save and restore information. This ContentProvider is used by the main application, as well as a couple of services, but all of them are in the same apk, and all the services live in the default (main) process.

我的内容提供者在清单中这样声明:

My content provider is declared like this in my manifest :

    <provider android:name="sample.provider.SampleProvider"
              android:authorities="sample.provider"
              android:exported="false"
              android:enabled="true">
    </provider>

我的一个类在URI上注册为观察者,并且在通知更改时,我正在直接查询提供程序以更新内部值.

One of my classes is registered as an observer on a URI, and when a change is notified, I'm querying the provider directly to update the internal value.

@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
    if (uri == null) {
        return;
    }
    try {
        Cursor updated = mContentResolver.query(uri, null, null, null, null);
        // ... working with the cursor here
    } catch (Exception e) {
        e.printStackTrace();
    }
}

此代码始终会失败,但以下情况除外

This code always failes, with the following exception

java.lang.SecurityException: Permission Denial: reading sample.provider.SampleProvider uri 
    content://sample.provider/infos/FOO from pid=0, uid=1000 requires the provider be 
    exported, or grantUriPermission()
      at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
      at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
      at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
      at android.content.ContentResolver.query(ContentResolver.java:491)
      at android.content.ContentResolver.query(ContentResolver.java:434)
      at sample.foo.Bar.onChange(Bar.java:331)
      at android.database.ContentObserver.onChange(ContentObserver.java:145)
      at android.database.ContentObserver.dispatchChange(ContentObserver.java:196)
      at android.database.ContentObserver.-wrap0(ContentObserver.java)
      at android.database.ContentObserver$Transport.onChange(ContentObserver.java:231)
      at android.database.IContentObserver$Stub.onTransact(IContentObserver.java:62)
      at android.os.Binder.execTransact(Binder.java:453)

请注意,当我在清单中使用exported="true"时,一切正常

Note that when I use exported="true" in the manifest, everything works fine

推荐答案

在研究了这个问题之后,我发现观察者是从另一个应用程序调用的(我的猜测是,操作系统直接称为观察者),这意味着当我进行查询,即使我正在运行自己的代码,我也不在自己的应用程序上下文中.

After investigating this issue, I discovered that my observer was called from another application (my guess is, it's the OS directly cally my observer), meaning that when I do the query I'm not in the context of my own app, even if it's my own code being run.

我通过在对象中创建处理程序并在检测到所观察到的URI发生更改时发送消息来解决此问题

I fixed this issue by creating a handler in my object, and sending a message when I detect a change on the observed URI

这篇关于从我自己的应用程序查询我自己的ContentProvider时获得权限拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆