Question...
I'm trying to implement runtime permissions in Android Marshmallow while maintaining backwards compatibility. I've done this successfully before, I'm not sure what's different this time. I've tested it on a physical Note 5 (running Mallow) and using the emulator set up on Marshmallow, neither works.
I realize "it doesn't work" isn't super helpful, but I don't know what else to say, nothing happens. The app doesn't crash it just hangs after requestPermissions(perms, 222)
is called.
What am I doing wrong?
Details...
Relevant parts of my activity:
public class HomeActivity extends Activity implements View.OnClickListener {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_home_scancertificate:
if (ContextCompat.checkSelfPermission(HomeActivity.this, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED){
showNoPermDialog();
}else {
AppData.ActionType = ActionType.SCAN_CERTIFICATE;
intent = new Intent(HomeActivity.this, CaptureActivity.class);
startActivity(intent);
}
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.e("HomeActivity", "Permissions results...");
switch (requestCode) {
case 222: {
boolean granted = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
Log.e("HomeActivity", granted?"granted permission":"denied permission");
AppData.ActionType = ActionType.SCAN_POSTCARD;
intent = new Intent(HomeActivity.this, CaptureActivity.class);
startActivity(intent);
}
}
}
public void getCamPerm(){
Log.e("HomeActivity", "Build version: "+Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT >= 23) {
Log.e("HomeActivity", "Getting permissions");
String[] perms = new String[]{Manifest.permission.CAMERA};
requestPermissions(perms, 222);
}
}
public void showNoPermDialog(){
if (Build.VERSION.SDK_INT >= 23) {
boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);
String status = showRationale ? "showing rationale" : "skipping rationale";
Log.e("HomeActivity", status);
if (showRationale) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Need permission");
alertDialogBuilder
.setMessage("App requires camera permission for the use of the scanner.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
getCamPerm();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}else getCamPerm();
}else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Need permission");
alertDialogBuilder
.setMessage("App requires permission to use the camera. You have disabled camera permission. Please re-enable this permission thru Settings -> Apps -> Our Town -> Permissions.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
}
When run, only these are logged (onRequestPermissionsResult()
is never called at all)
E/HomeActivity: skipping rationale
E/HomeActivity: Build version: 23
E/HomeActivity: Getting permissions
Manifest includes: <uses-permission android:name="android.permission.CAMERA" />
EDIT
Only on the first time I click the button, this shows up in the Logcat and seems to be triggered by the call to shouldShowRequestPermissionRationale()
08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.997 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
EDIT 2
I copied my same original code from the Activity it was in and moved it to the splash activity and it works fine there. It can stay there for now, but If anyone knows what's going on input is still appreciated.