Permission Denial:开放提供者 Android Studio
Posted
技术标签:
【中文标题】Permission Denial:开放提供者 Android Studio【英文标题】:Permission Denial: opening provider Android Studio 【发布时间】:2018-09-04 01:15:20 【问题描述】:我是内容提供者的新手。当我试图从一个应用程序获取信息到另一个应用程序时,我得到一个权限被拒绝:打开提供程序错误。我试图通过在我的一个应用程序(App1)中创建一个内容提供程序来做到这一点:
public class DroneProvider extends ContentProvider
private static final String AUTHORITY = "com.x.x.adminapp";
private static final String BASE_PATH = "Drone";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH );
private static final int DRONE = 1;
private static final int DRONE_NAME = 2;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
uriMatcher.addURI(AUTHORITY,BASE_PATH, DRONE);
uriMatcher.addURI(AUTHORITY,BASE_PATH + "/#",DRONE_NAME);
我的 Provider 类中的查询函数是:
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1)
Cursor cursor;
switch (uriMatcher.match(uri))
case DRONE:
cursor = database.query(MyDBHandler.TABLED_NAME,MyDBHandler.ALL_DRONE_COLUMNS,
s,null,null,null,MyDBHandler.COLUMN_DNAME +" ASC");
break;
default:
throw new IllegalArgumentException("This is an Unknown URI " + uri);
cursor.setNotificationUri(getContext().getContentResolver(),uri);
return cursor;
我在 Manifest 中添加了权限:
<provider
android:name=".DroneProvider"
android:authorities="com.x.x.adminapp"
android:readPermission="com.x.x.adminapp.READ_DATABASE"
android:writePermission="com.x.x.adminapp.WRITE_DATABASE"
android:enabled="true"
android:exported="true"></provider>
在我的另一个应用程序(App2)中,我在我的活动中使用解析器从(App1)获取数据,如下所示:
private static final String AUTHORITY = "com.x.x.adminapp";
private static final String BASE_PATH = "Drone";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH );
private static final int DRONE = 1;
private static final int DRONE_NAME = 2;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
uriMatcher.addURI(AUTHORITY,BASE_PATH, DRONE);
uriMatcher.addURI(AUTHORITY,BASE_PATH + "/#",DRONE_NAME);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
ContentResolver contentResolver = getContext().getContentResolver();
String sortOrder = "name ASC";
Cursor cursor = contentResolver.query(CONTENT_URI,null,null,null,sortOrder);
if (cursor!= null && cursor.getCount()>0)
cursor.moveToFirst();
Drone drone ;
Marker m;
LatLng latLng;
do
drone = new Drone();
drone.setName(cursor.getString(0));
latLng = new LatLng(0.0,0.0);
m = googleMap.addMarker(new MarkerOptions().position(latLng).title(drone.getName()));
Drones.add(m);
while (cursor.moveToNext());
我在 App2 Manifest 中添加了权限:
<uses-permission android:name="com.x.x.adminapp.READ_DATABASE" />
<uses-permission android:name="com.x.x.adminapp.WRITE_DATABASE" />
但我得到了这个错误:
java.lang.SecurityException: Permission Denial: 打开提供者 com.x.x.adminapp.DroneProvider 来自 过程记录46a1928 24678:com.x.x.myapplication11/u0a165 (pid=24678, uid=10165) 需要 com.x.x.adminapp.READ_DATABASE 或 com.x.x.adminapp.WRITE_DATABASE
提前感谢您的帮助:)
【问题讨论】:
App1 需要用<permission>
元素定义那些自定义权限,并且App1 需要在App2 之前安装。
我做了,还是同样的问题@CommonsWare
【参考方案1】:
您需要定义任何自定义权限:
https://developer.android.com/guide/topics/permissions/defining.html
<permission
android:name="com.x.x.adminapp.WRITE_DATABASE"
android:label="Write Database"
android:description="Write to my database."
android:protectionLevel="normal" />
<permission
android:name="com.x.x.adminapp.READ_DATABASE"
android:label="Read Database"
android:description="Read from my database."
android:protectionLevel="normal" />
注意保护级别“正常”,这意味着任何应用程序都可以请求并被授予该权限。我建议您将其更改为“签名”,这样只有您的应用才能获得许可。不用说,两个应用程序都需要使用相同的密钥进行签名。 :)
【讨论】:
以上是关于Permission Denial:开放提供者 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章
Android Permission Denial:这需要 android.permission.INTERACT_ACROSS_USERS_FULL
Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
即使将 <uses-permission> 用于 SET_ALARM,也会获得 Permission Denial?
已解决Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
使用GAPID报错Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
使用GAPID报错Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS