我是Content Provider的新手。因为我正在尝试从应用程序获取信息到另一个应用程序,但我得到了拒绝权限:打开提供程序错误。我尝试通过在我的某个应用程序(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来自 ProcessRecord {46a1928 24678:com.x.x.myapplication11 / u0a165}(pid = 24678, uid = 10165)需要com.x.x.adminapp.READ_DATABASE或 com.x.x.adminapp.WRITE_DATABASE
提前感谢您的帮助:)
答案 0 :(得分: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" />
注意保护级别&#34;正常&#34;这意味着任何应用程序都可以请求并被授予该权限。我建议你把它改成&#34;签名&#34;因此,只有您的应用可以被授予权限。不言而喻,两个应用程序都需要使用相同的密钥进行签名。 :)