-1

I just figured out how to have the android app post a dialog box asking for permission and to only execute a specific command when the user hits allow. But I am stuck on having the app restart after that, due to the android bug the app doesn't have permission unless restarted.

Code like doesn't seem to be working. The app reboots but leaves off where it was at; keeping the same preferences assigned by the device on the first launch.

// Schedule start after 1 second
        PendingIntent pi = PendingIntent.getActivity(this, 0, getIntent(), PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC, System.currentTimeMillis() + 10000, pi);

        // Stop now
        System.exit(0);

Is there a different way to restart the app, one that would work to allow the phone to tell the app it has permission to do certain things.

TheSwindler44
  • 177
  • 1
  • 10
  • 1
    You do not need to restart the app to get the permission. There is no such Android bug. Can you post more information on why you believe such a bug exists? – Bryan Herbst Jul 29 '16 at 15:05
  • Don't restart the app, you just need to ask for permission at runtime and make sure it doesn't crash if the permission isn't granted. – Shark Jul 29 '16 at 15:07
  • I have read a few people posts saying the problem with the app not changing permissions after being allowed is the bug. The only way to have this change is to just completely restart the app. – TheSwindler44 Jul 29 '16 at 15:09

1 Answers1

1

Asking for permissions in run time work asynchronous so you should override onRequestPermissionsResult() in the activity where you are asking for permissions. Like this:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {


    switch (requestCode) {
        case Constants.YOUR_REQUEST_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted. do your stuff here
            } else {
                // Permission denied - Show a message to inform the user that this app only works
                // with these permissions granted

            }
            return;
        }

    }
}
Miguel Benitez
  • 2,322
  • 10
  • 22