rahul
rahul

Reputation: 87

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2

Add Event in Calendar view. when add event in calendar then give below Permission error :

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

Give Permission in android Manifest file. :

My code is below:

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

how to Give Permission of Read and Write to add event.please guide me this permission.

How to add event in Calendar synchronization with Mobile Calendar.

Upvotes: 1

Views: 4513

Answers (2)

Roberto Gimenez
Roberto Gimenez

Reputation: 304

First of all, you need to add permissions to your manifest.xml For calendar, you need to add this

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

At runtime, you need to ask for permission again. So, you can create some functions to check it. Declare a vble for your callback

final int callbackId = 42;

and call to your function for checking permissions

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

And this is the function to declare:

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

Upvotes: 2

Jawad Ahmed
Jawad Ahmed

Reputation: 306

You need manifest and runtime permissions to do that:

Refer to this https://developer.android.com/training/permissions/requesting

Upvotes: 0

Related Questions