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.
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;
}
}
}