Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Utility {
- public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
- public static final int MY_PERMISSIONS_PHONE_CALL = 123;
- @TargetApi(Build.VERSION_CODES.M)
- public static boolean checkPermission(final Context context) {
- int currentAPIVersion = Build.VERSION.SDK_INT;
- if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
- if (ContextCompat.checkSelfPermission(context,
- Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
- // show a dialog
- }).show();
- }
- else {
- ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
- }
- return false;
- }
- if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CALL_PHONE)) {
- Log.v("TAG", "Permission is granted");
- new AlertDialog.Builder(context).setMessage("You need to enable permissions to use this feature").setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // navigate to settings
- ((Activity) context).startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
- }
- }).setNegativeButton("Go back", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // leave?
- ((Activity) context).onBackPressed();
- }
- }).show();
- }
- else {
- ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_PHONE_CALL);
- }
- return false;
- } else {
- return true;
- }
- } else {
- return true;
- }
- }
- }
- public class SecondAlertDialogFragment extends DialogFragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle args) {
- .......
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- // ListView Clicked item index
- Bundle args = getArguments();
- args = getArguments();
- int itemPosition = args.getInt("position");
- switch (itemPosition) {
- case 0:
- phoneNumber = "12345";
- email="mail.com";
- break;
- case 1:
- phoneNumber = "1246";
- email="mail2.com";
- break;
- case 2:
- phoneNumber = "8780";
- email="mail3.com";
- break;
- }
- if (position == 0) {
- dismiss();
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
- builder.setTitle("calling to " + phoneNumber);
- builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // Here I want to call the method but now sure how to do
- /*if (isPermissionGranted()) {
- call_action();
- }*/
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Do nothing
- dialog.dismiss();
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
- }
- });
- return rootView;
- }
- public void call_action(){
- Intent callIntent = new Intent(Intent.ACTION_CALL);
- callIntent.setData(Uri.parse("tel:" + phoneNumber));
- startActivity(callIntent);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case 1: {
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(getActivity().getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
- call_action();
- } else {
- Toast.makeText(getActivity().getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
- }
- return;
- }
- // other 'case' lines to check for other
- // permissions this app might request
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement