权限被拒绝:开放提供商 Android Studio

Permission Denial: opening provider Android Studio

我是内容提供商的新手。当我试图从一个应用程序获取信息到另一个应用程序时,我获得了许可 denial:opening 提供程序错误。我试图通过在我的一个应用程序 (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 class 中的查询函数是:

@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;
}

我在清单中添加了权限:

<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) 中,我在 activity 中使用解析器从 (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 清单中添加了权限:

<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: opening provider com.x.x.adminapp.DroneProvider from ProcessRecord{46a1928 24678:com.x.x.myapplication11/u0a165} (pid=24678, uid=10165) requires com.x.x.adminapp.READ_DATABASE or com.x.x.adminapp.WRITE_DATABASE

提前感谢您的帮助:)

您需要定义任何自定义权限:

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" />

注意保护级别"normal",这意味着任何应用程序都可以请求并被授予该权限。我建议您将其更改为 "signature",这样只有您的应用才能获得权限。不用说,这两个应用程序需要使用相同的密钥进行签名。 :)