【问题标题】:permission denial for content provider when called from within the same app从同一应用程序中调用时内容提供者的权限被拒绝
【发布时间】:2016-08-24 07:22:48
【问题描述】:

我只有一个应用程序。它有一个未导出的内容提供程序(我不希望其他应用访问该内容)到目前为止我一直在成功使用该内容提供程序。

我创建了一个内容观察器来更新 TextViewonChange 方法在内容更改并尝试重新查询该内容时被调用。那时它会收到如下所示的安全异常:

java.lang.SecurityException: Permission Denial: reading org.sil.lcroffline.data.DataProvider uri content://org.sil.lcroffline/users/by_account_name/5555544444 from pid=0, uid=1000 要求提供者是导出,或 grantUriPermission()

这是生成它的代码:

@Override
public void onChange(boolean selfChange, Uri uri) {
    Cursor c = null;
    try {
        c = mContentResolver.query(UserEntry.buildUserPhoneUri(mAccount.name), null, null, null, null);
        // do stuff with the data in the cursor
    } finally {
        if (c != null) c.close();
    }
}

URI 看起来格式正确,我认为在内容提供程序中匹配它没有问题。

上面的代码在应用程序内以编程方式调用时可以正常工作,只有当观察到的数据发生变化时才会发生异常。

如何从同一个应用程序中获得权限拒绝,以及如何解决此问题?

看清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.sil.lcroffline">

    <!-- To communicate with LCR -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Required because we're manually creating a new account. -->
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".authentication.LoginActivity"
            android:label="@string/app_name"
            android:exported="true">
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ReportActivity"
            android:parentActivityName=".MainActivity"></activity>
        <service android:name=".authentication.AuthenticatorService">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>
        <provider
            android:authorities="@string/data_authority"
            android:name=".data.DataProvider"
            android:exported="false" />
        <service
            android:name=".data.SyncService"
            android:exported="true"
            android:process=":sync">
            <intent-filter>
                <action android:name="android.content.SyncAdapter"/>
            </intent-filter>
            <meta-data android:name="android.content.SyncAdapter"
                android:resource="@xml/syncadapter" />
        </service>
    </application>

</manifest>

【问题讨论】:

  • 你的安卓操作系统是6.0吗?
  • 您可以尝试导出内容提供者 URI 吗?
  • 是的,它是 6.0.1,不,我不想导出内容提供者的任何部分。任何其他应用都不能访问所有内容。
  • @你有运行时权限吗?
  • IMO,android 将所有内容提供者数据存储在全局范围内,即从您应用的私有内存中存储,因此您需要申请访问(读取/修改)您自己的数据的权限。

标签: android security android-contentprovider


【解决方案1】:

原来这是我在ContentObserver 中没有使用Handler 造成的

当您不将处理程序传递给内容观察者时,将直接调用onChange 方法,而不是通过处理程序发布调用它的消息。这一切都很好,除了在这种情况下调用onChange 方法的进程是系统操作系统,它没有查询我的内容提供程序的必要权限。

为了解决这个问题,我更改了创建内容观察器的代码:

mUserObserver = new ContentObserver(null) {
    // override onChange methods here
}

到这里:

mUserObserver = new ContentObserver(new Handler()) {
    // override onChange methods here
}

this question 提示我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多