java.lang.SecurityException:权限拒绝:打开提供程序com.android.providers.calendar.CalendarProvider2

时间:2018-11-25 05:33:23

标签: android calendar android-permissions

在“日历”视图中添加事件。在日历中添加事件时,请在下面给出权限错误:

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{3620f5c 13430:google.com/u0a149} (pid=13430, uid=10149) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR

在Android清单文件中授予权限。 :

    

我的代码如下:

  calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                String date =(i2 + "/" + (i1 + 1) + "/" + i);
                showAlertDialog(date);

            }
        });

    }

    public void showAlertDialog(final String dateString1) {


        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.alert_dialog_item, null);


        final EditText etTitle = (EditText) dialogView.findViewById(R.id.etTitle);
        final EditText etEvent = (EditText) dialogView.findViewById(R.id.etEvent);

        final EditText etEventDate = (EditText) dialogView.findViewById(R.id.etStartDate);

        //final EditText etTime = (EditText) dialogView.findViewById(R.id.etTime);

        final EditText etEndDate = (EditText) dialogView.findViewById(R.id.etEndDate);

        Calendar cal = Calendar.getInstance();

        long date1 = cal.getTimeInMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy, HH:mm a");
        String dateString = dateFormat.format(date1);

        etEventDate.setText(dateString);

        long date2 = cal.getTimeInMillis() + 1000 * 60 * 60;
        String endDate = dateFormat.format(date2);
        etEndDate.setText(endDate);


        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
        builder.setTitle("Add Event");
        builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                String title = etTitle.getText().toString();
                String eventDescription = etEvent.getText().toString();
                String startDate = etEventDate.getText().toString();
                String endDate = etEndDate.getText().toString();

                //ContactsContract.CommonDataKinds.Event event = new ContactsContract.CommonDataKinds.Event(Color.RED,startDate,eventDescription);

                try {

                    cr = getApplicationContext().getContentResolver();
                    cv = new ContentValues();
                    cv.put(CalendarContract.Events.DTSTART, startDate);
                    cv.put(CalendarContract.Events.DTEND, endDate);
                    cv.put(CalendarContract.Events.TITLE, title);
                    cv.put(CalendarContract.Events.DESCRIPTION, eventDescription);
                    cv.put(CalendarContract.Events.CALENDAR_ID, 1);
                    cv.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC/GMT+5:30");
                    cv.put(CalendarContract.Events.CALENDAR_COLOR_KEY, Color.RED);

                    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, cv);

                    if (ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
                            && ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

                    }else if(ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED
                            && ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){

                       // ContentResolver contentResolver = getContentResolver();
                        //ContentValues contentValues = new ContentValues();


                        long eventID = Long.parseLong(uri.getLastPathSegment());

                        Toast.makeText(Event.this, (int) eventID, Toast.LENGTH_SHORT).show();

                    }



                }catch(Exception ex){

                    ex.printStackTrace();
                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                builder.setCancelable(true);
            }
        });

        builder.show();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {


        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_WRITE_CALENDAR:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(this, " Permission is Granted", Toast.LENGTH_SHORT).show();

                } else {
                    //code for deny
                }
                break;
        }
    }

如何授予读写权限以添加事件。请指导我该权限。

如何在与移动日历的日历同步中添加事件。

2 个答案:

答案 0 :(得分:0)

您需要清单和运行时权限才能这样做:

请参阅此https://developer.android.com/training/permissions/requesting

答案 1 :(得分:0)

首先,您需要向manifest.xml添加权限 对于日历,您需要添加此

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

在运行时,您需要再次请求许可。 因此,您可以创建一些函数来检查它。为您的回调声明一个vble

final int callbackId = 42;

并调用您的函数以检查权限

checkPermission(callbackId, Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR);

这是声明的功能:

private void checkPermission(int callbackId, String... permissionsId) {
    boolean permissions = true;
    for (String p : permissionsId) {
        permissions = permissions && ContextCompat.checkSelfPermission(this, p) == PERMISSION_GRANTED;
    }

    if (!permissions)
        ActivityCompat.requestPermissions(this, permissionsId, callbackId);
}