1. Download our Official Android App: Forums for Android!

Apps Android M permissions

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

  1. new_andro

    new_andro Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 13, 2016

    Mar 13, 2016
    2
    0
    5
    Male
    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
     

    Advertisement

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

    Mikestony ~30% Carbon Black ±
    Moderator
    Rank:
     #6
    Points:
    3,118
    Posts:
    29,801
    Joined:
    Sep 10, 2010

    Sep 10, 2010
    29,801
    24,467
    3,118
    Male
    Next to you
    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
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 13, 2016

    Mar 13, 2016
    2
    0
    5
    Male
    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
    Rank:
     #6
    Points:
    3,118
    Posts:
    29,801
    Joined:
    Sep 10, 2010

    Sep 10, 2010
    29,801
    24,467
    3,118
    Male
    Next to you
    Hmm, I'll pass this up the chain, but in the meantime, hopefully someone can assist with your issue ;)
     
  5. Unforgiven

    Unforgiven OK Google
    Moderator
    Rank:
     #1
    Points:
    4,238
    Posts:
    36,298
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    36,298
    42,397
    4,238
    Male
    Douglas, MA
    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.

Share This Page

Loading...