Reputation: 478
I'm sure this has a very simple explanation, but it has me stumped. With the new updates to API 23, you must declare permissions both in the manifest and in the actual code. Yet the request code for what I am trying to do does not appear.
Here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ryanfolz.textscrabble">
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And in my first activity I call this method:
if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS")
== PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);
}
However, the "REQUEST_CODE_ASK_PERMISSIONS" doesn't link to any numeric value. In fact, there are no request codes the appear in the autocomplete either.
Does anyone know why this code does not appear and how to fix it?
Upvotes: 1
Views: 4978
Reputation: 176
I had a similar problem, and when I had to request permission I simply defined and Integer called REQUEST_CODE. Like:
final int REQUEST_CODE = 100;
Then I called:
ActivityCompat.requestPermissions(MainActivity.this, new String[]{"android.permission.READ_SMS"}, REQUEST_CODE);
Then onRequestPermission call, I handle if request code equals my REQUEST_CODE integer.
So just define an integer to call for a permission.
May not be the exact sollution you are looking for, but it works.
Upvotes: 1
Reputation: 2593
You have to assign an arbitrary value to REQUEST_CODE_ASK_PERMISSIONS
. you should declare REQUEST_CODE_ASK_PERMISSIONS
as a constant with an arbitrary numerical value like
public final int REQUEST_CODE_ASK_PERMISSIONS = 1001;
this value is used to know which permissions are allowed and manage in onRequestPermissionsResult
. i.e, you asks permissions on two different situations in same activity, and you have only one onRequestPermissionsResult
method. So you have to distinguish which result is catched in onRequestPermissionsResult
.
So if you have two requests, say for SMS, and for WAKE_LOCK, you are requesting with two request codes, say:
public final int REQUEST_CODE_SMS_PERMISSIONS = 1001;
public final int REQUEST_CODE_WAKELOCK_PERMISSIONS = 1002;
So that you can manage the result in onRequestPermissionsResult
like this
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode){
case REQUEST_CODE_SMS_PERMISSIONS:
//Code for handling SMS Permission results
break;
case REQUEST_CODE_WAKELOCK_PERMISSIONS:
//Code for handling WAKE_LOCK Permission results
break;
}
}
Upvotes: 2