1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android M permissions

Discussion in 'Android Development' started by new_andro, Mar 13, 2016.

  1. new_andro

    new_andro Lurker
    Thread Starter

    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 (Text):
    1.  
    2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    3.  
    4.   if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
    5.         theDate = data.getLongExtra("theDate", theDate);
    6.         if (theDate != 0) {
    7.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
    8.                 if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, Manifest.permission.READ_CALENDAR)) {
    9.                     Toast.makeText(getApplicationContext(),"Please",Toast.LENGTH_LONG).show();
    10.                 }
    11.                 ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.READ_CALENDAR}, 1);
    12.             } else
    13.                 doTheCalendarRead();
    14.         } else {
    15.             Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_LONG).show();
    16.         }
    17.     }
    18. }
    19.  
    20. public void onRequestPermissionsResult(int requestCode,
    21.                                        @NonNull String permissions[], @NonNull int[] grantResults) {
    22.     switch (requestCode) {
    23.         case 1: {
    24.             if (grantResults.length == 0  || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
    25.                 Toast.makeText(getApplicationContext(),"Oh well...",Toast.LENGTH_LONG).show();
    26.             } else
    27.                 doTheCalendarRead();
    28.         }
    29.         default: super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    30.     }
    31. }
    32.  
    33. and the actual permission-required action:
    34. private void doTheCalendarRead() {
    35.     String[] projection = new String[]{CalendarContract.Events.TITLE, CalendarContract.Events.EVENT_LOCATION};
    36.  
    37.     Calendar startTime = Calendar.getInstance();
    38.     startTime.setTimeInMillis(theDate);
    39.     startTime.set(Calendar.HOUR_OF_DAY, 0);
    40.     startTime.set(Calendar.MINUTE, 0);
    41.     startTime.set(Calendar.SECOND, 0);
    42.     startTime.set(Calendar.MILLISECOND, 0);
    43.  
    44.     Calendar endTime = Calendar.getInstance();
    45.     endTime.setTimeInMillis(theDate);
    46.     endTime.set(Calendar.HOUR_OF_DAY, 23);
    47.     endTime.set(Calendar.MINUTE, 59);
    48.     endTime.set(Calendar.SECOND, 59);
    49.     endTime.set(Calendar.MILLISECOND, 999);
    50.  
    51.     String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";
    52.  
    53.     Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
    54.  
    55.     if (cursor.moveToFirst()) {
    56.         do {
    57.             Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
    58.         } while ( cursor.moveToNext());
    59.     }
    60. }
    61.  
    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
     


    #1 new_andro, Mar 13, 2016
    Last edited: Mar 15, 2016
  2. Mikestony

    Mikestony ~30% Carbon Black ±
    Moderator

    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 :
    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
     
  3. new_andro

    new_andro Lurker
    Thread Starter

    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.
     
  4. Mikestony

    Mikestony ~30% Carbon Black ±
    Moderator

    Hmm, I'll pass this up the chain, but in the meantime, hopefully someone can assist with your issue ;)
     
  5. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    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.:(
     
    Mikestony likes this.
Loading...
Similar Threads - Android permissions
  1. GeorgeFrancis Lee
    Replies:
    0
    Views:
    71
  2. TheOuz
    Replies:
    6
    Views:
    260
  3. Dmitry Zlenko
    Replies:
    0
    Views:
    419
  4. azhar123454
    Replies:
    1
    Views:
    351
  5. Rgarner
    Replies:
    7
    Views:
    993
  6. Octane_Tech
    Replies:
    1
    Views:
    2,089
  7. Luna2018
    Replies:
    0
    Views:
    535
  8. kharoon
    Replies:
    9
    Views:
    1,436
  9. Chandra Shekhar Mantha
    Replies:
    2
    Views:
    2,110
  10. minimum
    Replies:
    1
    Views:
    295

Share This Page

Loading...