Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android M permissions close my app

I check for the required permissions on my Log In screen. The dialog shows asking for the 4 permissions I need. All good, but when the dialog appears, the background becomes black and my app closes (doesn't crash, just closes). When I'm done with choosing the permissions I open the app again and it continues running from where it stopped. How can I make the app continue running while the permissions dialog is shown? I use a class which checks for permissions and call it in the Log In activity.

The class:

public abstract class RuntimePermissionsActivity extends AppCompatActivity {
private SparseIntArray mErrorString;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    mErrorString = new SparseIntArray();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    for (int permission : grantResults){
        permissionCheck = permissionCheck + permission;
    }
    if ((grantResults.length>0) && permissionCheck == PackageManager.PERMISSION_GRANTED){
        onPermissionsGranted(requestCode);
    }
    else {

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
    }
}

public void requestAppPermissions(final String[] requestedPermissions, final int requestCode){
    int stringId = 0;
    mErrorString.put(requestCode, stringId);
    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    boolean shouldShowRequestPermissionRationale = false;
    for (String permission : requestedPermissions){
        permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
        shouldShowRequestPermissionRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
    }
    if(permissionCheck != PackageManager.PERMISSION_GRANTED){
        if(shouldShowRequestPermissionRationale){
                    ActivityCompat.requestPermissions(RuntimePermissionsActivity.this, requestedPermissions, requestCode);
        }
        else {
            ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
            Toast.makeText(RuntimePermissionsActivity.this, "Please relaunch the application in order for the changes to take effect!", Toast.LENGTH_LONG).show();
        }
    }
    else {
        onPermissionsGranted(requestCode);
    }

}
public abstract void onPermissionsGranted(int requestCode);
}

The Log In activity call:

LoginActivity.super.requestAppPermissions(new
            String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
like image 373
N. Park Avatar asked Aug 09 '16 07:08

N. Park


People also ask

Can I turn off all app permissions?

To see a more comprehensive list of permissions, you can tap on the Apps & notifications screen, then tap App permissions. In this window, you can browse apps by the permissions they access, and turn off any you like.

Can I restrict Android app permissions?

Open Settings and tap Apps & notifications. Tap Permission manager to open the Android permission controller app. Click a specific permission from the app permissions list that you're interested in, like location. Here you'll see apps that have access to your location all the time or only while in use.


2 Answers

I faced same issue,check if activity android:noHistory="true" in your Manifest file.

like image 150
ankit jain Avatar answered Oct 27 '22 10:10

ankit jain


Same problem here.

ankit jain was on the track. Removing android:noHistory="true" fixed the N.Park problem.

Although in my case I needed that value to be true, or at least the behaviour that makes (I had a SplashScreen where I managed the permissions, and after moving on, I wanted that Activity out of my stack).

So:

  1. I deleted line android:noHistory="true"

  2. In the activity I manage the permission without problems

  3. After that, I move into the next activity with:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
like image 23
DanixDani Avatar answered Oct 27 '22 10:10

DanixDani