gpt4 book ai didi

android - 从同一应用程序内调用时内容提供者的权限被拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:20 24 4
gpt4 key购买 nike

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

我创建了一个内容观察器来更新 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 requires the provider be exported, or 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 看起来格式正确,我认为在内容提供程序中匹配它没有问题。

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

我怎样才能从同一个应用程序中获得拒绝权限,我该如何解决这个问题?

看 list :

<?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>

最佳答案

事实证明这是由于我没有在 ContentObserver 中使用 Handler 造成的

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

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

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

对此:

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

我是通过 this question 得到线索的

关于android - 从同一应用程序内调用时内容提供者的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39116879/

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