访问另一个应用程序的内容提供者时权限被拒绝
Permission denial while accessing content provider of another application
我用来了解内容提供商的教程
AppA: https://www.tutorialspoint.com/android/android_content_providers.htm
我创建了另一个我打算使用的应用程序,以访问教程应用程序的内容提供程序
AppB Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication2">
<uses-permission android:name="com.example.MyApplication.StudentProvider.READ_PERMISSION"/>
<uses-permission android:name="com.example.MyApplication.StudentProvider.WRITE_PERMISSION"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.permRead"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AppB Partial Code:
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
Cursor c = getContentResolver().query(uri,null,null,null,null);
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
我收到以下错误:
Caused by: java.lang.SecurityException: Permission Denial: opening provider
关于我为什么会收到此错误的任何想法?感谢您的帮助!
编辑: 我得到了解决方案! (//这行是新的 & //修改的行)
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
ContentResolver cr = getContentResolver(); //This line is new
Cursor c = cr.query(uri,null,null,null,null); //Line modified
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
现在我的 AppB 可以从 AppA 读取 ContentProvider,但是我仍然不明白为什么这会有所作为!
EDIT2: 已经用 ChangedManifest XML
替换了教程清单
我在你的 AndroidManifest.xml
(AppA) 中没有看到这个:
<provider
android:name="StudentsProvider"
android:authorities="com.example.MyApplication.StudentsProvider"
android:grantUriPermission="true" />
如果这没有帮助,它可能是运行时权限:
https://developer.android.com/training/permissions/requesting.html
编辑:使用以下内容更改 AppA AndroidManifest.xml
:https://pastebin.com/Kv5nGjsg
解决方案:在获得所有必要权限后,需要使用内容解析器来访问其他应用程序内容提供程序declared/requested/granted。
我用来了解内容提供商的教程
AppA: https://www.tutorialspoint.com/android/android_content_providers.htm
我创建了另一个我打算使用的应用程序,以访问教程应用程序的内容提供程序
AppB Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication2">
<uses-permission android:name="com.example.MyApplication.StudentProvider.READ_PERMISSION"/>
<uses-permission android:name="com.example.MyApplication.StudentProvider.WRITE_PERMISSION"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.permRead"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AppB Partial Code:
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
Cursor c = getContentResolver().query(uri,null,null,null,null);
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
我收到以下错误:
Caused by: java.lang.SecurityException: Permission Denial: opening provider
关于我为什么会收到此错误的任何想法?感谢您的帮助!
编辑: 我得到了解决方案! (//这行是新的 & //修改的行)
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
ContentResolver cr = getContentResolver(); //This line is new
Cursor c = cr.query(uri,null,null,null,null); //Line modified
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
现在我的 AppB 可以从 AppA 读取 ContentProvider,但是我仍然不明白为什么这会有所作为!
EDIT2: 已经用 ChangedManifest XML
替换了教程清单我在你的 AndroidManifest.xml
(AppA) 中没有看到这个:
<provider
android:name="StudentsProvider"
android:authorities="com.example.MyApplication.StudentsProvider"
android:grantUriPermission="true" />
如果这没有帮助,它可能是运行时权限:
https://developer.android.com/training/permissions/requesting.html
编辑:使用以下内容更改 AppA AndroidManifest.xml
:https://pastebin.com/Kv5nGjsg
解决方案:在获得所有必要权限后,需要使用内容解析器来访问其他应用程序内容提供程序declared/requested/granted。