如何修复安全例外权限被拒绝

时间:2019-06-17 11:09:01

标签: android google-calendar-api android-calendar

即使我已经提供了日历应用程序中的读取和写入权限,我也遇到了安全异常。

我是android编程的初学者,我们将不胜感激。

我尝试了多次清理和重建项目,但问题仍然存在。

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cypherlabs.calendarapp">
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

代码:

   button.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
                findID();
                addCalendarEvent();
            }
        });
    }//signifies it is inside on create

    public void findID() {
        final String[] EVENT_PROJECTION = new String[]{
                Calendars._ID,                           // 0
                Calendars.ACCOUNT_NAME,                  // 1
                Calendars.CALENDAR_DISPLAY_NAME,         // 2
                Calendars.OWNER_ACCOUNT                  // 3
        };
        final int PROJECTION_ID_INDEX = 0;
        final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
        final int PROJECTION_DISPLAY_NAME_INDEX = 2;
        final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;

        Cursor cur = null;
        ContentResolver cr = getContentResolver();
        Uri uri = Calendars.CONTENT_URI;
        String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
                + Calendars.ACCOUNT_TYPE + " = ?) AND ("
                + Calendars.OWNER_ACCOUNT + " = ?))";
        String[] selectionArgs = new String[]{"hera@example.com", "com.example",
                "hera@example.com"};
// Submit the query and get a Cursor object back.
        cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);

        while (cur.moveToNext())

        {
            calID = cur.getLong(PROJECTION_ID_INDEX);
            displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
            accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
            ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
        }
    }

    public void addCalendarEvent() {

        long startMillis = 0;
        long endMillis = 0;
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2019, 6, 17, 7, 30);
        startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2019, 6, 17, 8, 45);
        endMillis = endTime.getTimeInMillis();


        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        values.put(Events.DTSTART, startMillis);
        values.put(Events.DTEND, endMillis);
        values.put(Events.TITLE, "Jazzercise");
        values.put(Events.DESCRIPTION, "Group workout");
        values.put(Events.CALENDAR_ID, calID);
        values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
        Uri uri = cr.insert(Events.CONTENT_URI, values);
        long eventID = Long.parseLong(uri.getLastPathSegment());
        values.put(Reminders.MINUTES, 15);
        values.put(Reminders.EVENT_ID, eventID);
        values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
        Uri reminders = cr.insert(Reminders.CONTENT_URI, values);
    }

logcat如下:

2019-06-17 16:26:47.888 14274-14274/com.cypherlabs.calendarapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.cypherlabs.calendarapp, PID: 14274
    java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{a5fb990 14274:com.cypherlabs.calendarapp/u0a130} (pid=14274, uid=10130) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
        at android.os.Parcel.createException(Parcel.java:1950)
        at android.os.Parcel.readException(Parcel.java:1918)
        at android.os.Parcel.readException(Parcel.java:1868)
        at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4162)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:6019)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2592)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1832)
        at android.content.ContentResolver.query(ContentResolver.java:789)
        at android.content.ContentResolver.query(ContentResolver.java:754)
        at android.content.ContentResolver.query(ContentResolver.java:712)
        at com.cypherlabs.calendarapp.MainActivity.findID(MainActivity.java:69)
        at com.cypherlabs.calendarapp.MainActivity$1.onClick(MainActivity.java:42)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25889)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12271)
        at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12678)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:358)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3322)
        at android.os.Binder.execTransact(Binder.java:731)

0 个答案:

没有答案