Skip to content

Instantly share code, notes, and snippets.

@prasad091
Created March 14, 2017 05:39
Show Gist options
  • Save prasad091/1949147958fc789507f123beaf47f072 to your computer and use it in GitHub Desktop.
Save prasad091/1949147958fc789507f123beaf47f072 to your computer and use it in GitHub Desktop.
package com.kgisl.sharetruck.hub.util;
/**
* Created by admin on 14-03-2017.
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
/**
* Copyright (C) 2016 Mikhael LOPEZ
* Licensed under the Apache License Version 2.0
*/
public class PermissionsUtils {
//region MY_PERMISSIONS_REQUEST
public static final int MY_PERMISSIONS_REQUEST_EXAMPLE = 1;
//endregion
public static boolean checkAndRequest(@NonNull final Activity activity, @NonNull final String permission, final int requestCode, String messagePermission, DialogInterface.OnClickListener onCancelListener) {
boolean result = false;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(messagePermission).setPositiveButton(activity.getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
}
}).setNegativeButton(activity.getResources().getString(android.R.string.cancel), onCancelListener).show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
// MY_PERMISSIONS_REQUEST is an app-defined int constant.
// The callback method gets the result of the request.
}
} else {
result = true;
}
return result;
}
public static void startInstalledAppDetailsActivity(final Activity context) {
if (context == null) {
return;
}
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment