• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android M permissions

new_andro

Lurker
Mar 13, 2016
2
0
I followed the instructions in this page: https://developer.android.com/training/permissions/requesting.html It looks like I didn't miss a thing but still I'm getting the red zigzag saying: Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`

My flow is simple: I call an intent to get a date and when I get one I do the following:

Code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
        theDate = data.getLongExtra("theDate", theDate);
        if (theDate != 0) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, Manifest.permission.READ_CALENDAR)) {
                    Toast.makeText(getApplicationContext(),"Please",Toast.LENGTH_LONG).show();
                }
                ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.READ_CALENDAR}, 1);
            } else
                doTheCalendarRead();
        } else {
            Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_LONG).show();
        }
    }
}

public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length == 0  || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(),"Oh well...",Toast.LENGTH_LONG).show();
            } else
                doTheCalendarRead();
        }
        default: super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

and the actual permission-required action:
private void doTheCalendarRead() {
    String[] projection = new String[]{CalendarContract.Events.TITLE, CalendarContract.Events.EVENT_LOCATION};

    Calendar startTime = Calendar.getInstance();
    startTime.setTimeInMillis(theDate);
    startTime.set(Calendar.HOUR_OF_DAY, 0);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.SECOND, 0);
    startTime.set(Calendar.MILLISECOND, 0);

    Calendar endTime = Calendar.getInstance();
    endTime.setTimeInMillis(theDate);
    endTime.set(Calendar.HOUR_OF_DAY, 23);
    endTime.set(Calendar.MINUTE, 59);
    endTime.set(Calendar.SECOND, 59);
    endTime.set(Calendar.MILLISECOND, 999);

    String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";

    Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);

    if (cursor.moveToFirst()) {
        do {
            Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
        } while ( cursor.moveToNext());
    }
}
The line:

Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null)

gives me the red zigzag I mentioned above, the thing is, I already asked for the permission before calling this function. The only way to get rid of the red zigzag is asking for the permission just above it but it's async and the result is retrieved in a callback... What am I missing?

ps: I'm new here and didn't find how to format the code above to show as a code... please excuse me
 
Last edited:
Hello there and welcome!
Sorry I cannot help you with your issue, that is all French to me :D
However, you can format your text to show as a code with :
Code:
, [PHP], [HTML] - Programming Code Display[/B][/B][/B]
[B][B][B]Displays text in one of several programming languages, highlighting the syntax where possible.

Example:
[CODE]General
code
PHP:
echo $hello . 'world';
http://androidforums.com/help/bb-codes

Just highlight the text you want, then hit the "insert" icon in your reply box and a dropdown will come up:
Capture.PNG
 
Upvote 0
Thanks but when I put the code inside the code tag and want to post my changes, I get the following popup:
Your content can not be submitted. This is likely because your content is spam-like or contains inappropriate elements. Please change your content or try again later. If you still have problems, please contact an administrator.
 
Upvote 0
Thanks but when I put the code inside the code tag and want to post my changes, I get the following popup:
Your content can not be submitted. This is likely because your content is spam-like or contains inappropriate elements. Please change your content or try again later. If you still have problems, please contact an administrator.
Hmm, I'll pass this up the chain, but in the meantime, hopefully someone can assist with your issue ;)
 
Upvote 0
Thanks but when I put the code inside the code tag and want to post my changes, I get the following popup:
Your content can not be submitted. This is likely because your content is spam-like or contains inappropriate elements. Please change your content or try again later. If you still have problems, please contact an administrator.
I edited in the quote tags. Like Mike, I'm a bit flummoxed as to why it was blocked. Sometimes it just doesn't get along with the spam filters.:(
 
  • Like
Reactions: Mikestony
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones