当前位置: 首页 > 知识库问答 >
问题:

java.lang.SecurityException:权限拒绝:实现内容提供程序时打开提供程序

闻人锦
2023-03-14

我遇到了问题,我尝试在我的comsumer应用程序中实现内容提供程序:

这是我从App A(提供商)获得的Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.erlanggakmoekasan.imovie">
    <permission android:name="com.erlanggakmoekasan.imovie.READ_DATABASE" android:protectionLevel="normal" />
    <permission android:name="com.erlanggakmoekasan.imovie.WRITE_DATABASE" android:protectionLevel="normal" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <receiver android:name=".widgets.MoviesWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/movies_widget_info" />
        </receiver>

        <activity android:name=".activity.TvShowActivitySearch" />
        <activity android:name=".activity.MovieActivitySearch" />
        <activity android:name=".activity.TvShowActivity" />
        <activity
            android:name=".activity.MainActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.MovieActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activity.MainActivity" />
        </activity>
        <provider
            android:authorities="com.erlanggakmoekasan.imovie"
            android:name=".provider.MoviesProvider"
            android:exported="true"
            android:readPermission="com.erlanggakmoekasan.imovie.READ_DATABASE"
            android:writePermission="com.erlanggakmoekasan.imovie.WRITE_DATABASE" />
        <service
            android:name="com.erlanggakmoekasan.imovie.widgets.StackWidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS" />

    </application>

</manifest>

这是我从App B(消费者)获得的Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.erlanggakmoekasan.favoritemovies">

    <uses-permission android:name="android.permission.INTERNET"/>
    <permission android:name="com.erlanggakmoekasan.imovie.READ_DATABASE" />
    <permission android:name="com.erlanggakmoekasan.imovie.WRITE_DATABASE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

应用程序A得我得提供商:

package com.erlanggakmoekasan.imovie.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import com.erlanggakmoekasan.imovie.utils.MoviesHelper;

import static com.erlanggakmoekasan.imovie.utils.MoviesContract.AUTHORITY;
import static com.erlanggakmoekasan.imovie.utils.MoviesContract.MoviesColumns.CONTENT_URI;
import static com.erlanggakmoekasan.imovie.utils.MoviesContract.MoviesColumns.TABLE_NAME;

public class MoviesProvider extends ContentProvider {
    private static final int MOVIES = 1;
    private static final int MOVIES_ID = 2;
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {

        // content://com.erlanggastudio.imovie/movies
        sUriMatcher.addURI(AUTHORITY, TABLE_NAME, MOVIES);

        // content://com.erlanggastudio.imovie/movies/id
        sUriMatcher.addURI(AUTHORITY,
                TABLE_NAME+ "/#",
                MOVIES_ID);
    }
    private MoviesHelper moviesHelper;

    @Override
    public boolean onCreate() {
        moviesHelper = MoviesHelper.getInstance(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@Nullable Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        moviesHelper.open();
        Cursor cursor;
        switch (sUriMatcher.match(uri)) {
            case MOVIES:
                cursor = moviesHelper.queryProvider();
                break;
            case MOVIES_ID:
                cursor = moviesHelper.queryByIdProvider(uri.getLastPathSegment());
                break;
            default:
                cursor = null;
                break;
        }
        if (cursor!=null){

            cursor.setNotificationUri(getContext().getContentResolver(),uri);
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@Nullable Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@Nullable Uri uri, @Nullable ContentValues values) {
        moviesHelper.open();
        long added ;

        switch (sUriMatcher.match(uri)){
            case MOVIES:
                added = moviesHelper.insertProvider(values);
                break;
            default:
                added = 0;
                break;
        }

        if (added > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return Uri.parse(CONTENT_URI + "/" + added);
    }

    @Nullable
    public int delete(@Nullable Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        moviesHelper.open();
        int deleted;
        switch (sUriMatcher.match(uri)) {
            case MOVIES_ID:
                deleted =  moviesHelper.deleteProvider(uri.getLastPathSegment());
                break;
            default:
                deleted = 0;
                break;
        }

        if (deleted > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return deleted;
    }

    @Nullable
    public int update(@Nullable Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        moviesHelper.open();
        int movieUpdated ;
        switch (sUriMatcher.match(uri)) {
            case MOVIES_ID:
                movieUpdated =  moviesHelper.updateProvider(uri.getLastPathSegment(),values);
                break;
            default:
                movieUpdated = 0;
                break;
        }

        if (movieUpdated > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return movieUpdated;
    }
}
package com.erlanggakmoekasan.favoritemovies.activity;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.erlanggakmoekasan.favoritemovies.R;
import com.erlanggakmoekasan.favoritemovies.adapter.MoviesAdapter;
import com.erlanggakmoekasan.favoritemovies.callback.MoviesCallback;
import com.erlanggakmoekasan.favoritemovies.model.Movie;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import static com.erlanggakmoekasan.favoritemovies.entity.MappingHelper.mapCursorToArrayList;
import static com.erlanggakmoekasan.favoritemovies.utils.MoviesContract.MoviesColumns.CONTENT_URI;

public class MainActivity extends AppCompatActivity implements MoviesCallback {

    private DataObserver myObserver;
    private RecyclerView moviesList;
    private MoviesAdapter moviesAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        moviesList = findViewById(R.id.movie_list);
        moviesAdapter = new MoviesAdapter(this);
        moviesList.setLayoutManager(new LinearLayoutManager(this));
        moviesList.setHasFixedSize(true);
        moviesList.setAdapter(moviesAdapter);
        HandlerThread handlerThread = new HandlerThread("DataObserver");
        handlerThread.start();
        Handler handler = new Handler(handlerThread.getLooper());
        myObserver = new DataObserver(handler, this);
        getContentResolver().registerContentObserver(CONTENT_URI, true, myObserver);
        new getData(this, this).execute();

    }

    @Override
    public void postExecute(Cursor movie) {

        ArrayList<Movie> listMovie = mapCursorToArrayList(movie);
        if (listMovie.size() > 0) {
            moviesAdapter.setListMovies(listMovie);
        } else {
            Toast.makeText(this, "Tidak Ada data saat ini", Toast.LENGTH_SHORT).show();
            moviesAdapter.setListMovies(new ArrayList<Movie>());
        }
    }


    private static class getData extends AsyncTask<Void, Void, Cursor> {
        private final WeakReference<Context> weakContext;
        private final WeakReference<MoviesCallback> weakCallback;


        private getData(Context context, MoviesCallback callback) {
            weakContext = new WeakReference<>(context);
            weakCallback = new WeakReference<>(callback);
        }

        @Override
        protected Cursor doInBackground(Void... voids) {
            return weakContext.get().getContentResolver().query(CONTENT_URI, null, null, null, null);
        }

        @Override
        protected void onPostExecute(Cursor data) {
            super.onPostExecute(data);
            weakCallback.get().postExecute(data);
        }

    }

    static class DataObserver extends ContentObserver {

        final Context context;

        DataObserver(Handler handler, Context context) {
            super(handler);
            this.context = context;
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            new getData(context, (MainActivity) context).execute();
        }
    }
}

process:com.erlanggakmoekasan.favoritemovies,pid:27146 java.lang.runtimeException:无法启动活动ComponentInfo{com.erlanggakmoekasan.favoritemovies/com.erlanggakmoekasan.favoritemovies.activity.mainactivity}:java.lang.securityException:拒绝权限:从ProcessRecord{7b99e1ed0 27146:com.erlanggakmoekasan.imovie.provider.movies/u0a307}(PID=27146,

尝试调用GetContentResolver()时出错。RegisterContentObserver(CONTENT_URI,true,myObserver);

从app B有什么解决我问题的方法吗???

共有1个答案

程冥夜
2023-03-14

App B需要com.erlanggakmoekasan.imovie.read_databasecom.erlanggakmoekasan.imovie.write_database 元素。

此外,您还需要确保:

>

  • 应用程序A总是先于应用程序B安装,或者

    App B具有与App A相同的 元素,并且App A和App B都使用相同的签名密钥进行签名

    自定义权限在Android中并不那么好用。如果可能的话,把这两个APP组合成一个单独的APP。

  •  类似资料:
    • 我开始图像拾取器的意图使用: 在中,我获取所有选中图像的URI,启动在后台运行的作业并上传这些图像(https://github.com/yigit/android-priority-jobqueue)。但如果按下“后退”按钮并退出活动,则任何未启动的作业在运行并抛出异常时都无法访问选定的映像: java.lang.SecurityException:拒绝权限:从ProcessRecord{...

    • 我已经创建了一个自定义内容提供者,它将被更多的应用程序访问。我已经在我的提供者AndroidManifest中包含了权限标记。xml文件,在第二个应用程序中,我包含了uses-permissions标记,但没有成功。Logcat向我展示: 我也搜索过类似的问题,但似乎一切都是正确的。有什么想法吗?谢谢 这是我的提供者AndroidManifest。xml文件: 这是我的第二个应用程序Android

    • 并且我在App2清单中添加了权限: 但我得到了这个错误: java.lang.SecurityException:拒绝权限:从ProcessRecord{46a1928 24678:com.x.x.myApplication11/U0a165}(PID=24678,UID=10165)打开提供程序com.x.x.adminapp.read_database或com.x.x.adminapp.wri

    • java.lang.SecurityException:权限拒绝:从ProcessRecord{932688f 12849:mobi.hubbler.app/U0A158}(PID=12849,uid=10158)打开提供程序com.miui.gallery.provider.GalleryOpenProvider,而该提供程序不是从uid 10034导出的 试图从URI获取位图。这个问题只发生在

    • 我在尝试在Android emulator中执行应用程序升级时遇到了一些问题。该场景的流程来自一个活动,我将执行打开片段A的AsyncTask A,然后在AsyncTask A中,我将检查版本升级是否可用。 如果可用并且用户从片段A中选择了“OK”,我将转到AsyncTask B打开片段B,它向用户显示一条消息,说明正在进行升级。在AsyncTask B doInbackground()中,我将执

    • 我有两个app:app1和app2。 App2具有: paths.xml: App2在其活动中从App1接收获取映像URI的请求。确定URI后,App2活动执行以下操作: 从App2接收回结果后,App1执行以下操作: 在App1中,从App2返回时,我得到以下异常: java.lang.SecurityException:权限拒绝:从ProcessRecord{52a99eb0 3493:com