Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - android

I am trying to implement dark theme from fragment named as "Fragment_One" in android studio.
This is from that fragment file
if (!isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isChecked = true;
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isChecked = false;
}
It is giving error in MainActivity in the following line of onRequestPermissionsResult() method
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
The onRequestPermissionsResult() method is as follows:
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "permission granted on request", Toast.LENGTH_SHORT).show();
files= getAllFiles(this);
FragmentTransaction f_fragmentTransaction = getSupportFragmentManager().beginTransaction();
f_fragmentTransaction.replace(R.id.mainFragment, new Fragment_Two()).commit();
}
if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_WRITE_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]
{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
}
if (requestCode == 100) {
if (grantResults.length > 0
&& grantResults[0] + grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,"allow settings ",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"permission denied ",Toast.LENGTH_SHORT).show();
}
return;
}
}

Related

onShowFileChooser doesn't fire if permissions wasn't granted

My webView application is asking for permissions when user taps on file upload
#Override
public boolean onShowFileChooser(WebView view, ValueCallback < Uri[] > valueCallback, WebChromeClient.FileChooserParams fileChooserParams) {
Context context = getApplicationContext();
Log.d("LOG", "Triggered 1");
if (fileChooserCallback != null) {
fileChooserCallback.onReceiveValue(null);
}
fileChooserCallback = valueCallback;
String[] permissions = new String[] {
android.Manifest.permission.CAMERA,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE
};
Log.d("LOG", "Triggered 2");
if (!hasPermissions(context, permissions)) {
ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_CAMERA_WITH_STORAGE);
} else {
startPictureSelectionActivity();
}
return true;
}
private boolean hasPermissions(Context context, String...permissions) {
for (String permission: permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
if (requestCode == PERMISSION_CAMERA_WITH_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startPictureSelectionActivity();
}
} else if (requestCode == PERMISSION_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationCallback.invoke(locationOrigin, true, true);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
And upload works fine if users agrees to give requested permissions, but if he doesn't, application just doesn't react (just nothing happens, log is empty) on upload tap anymore until user restarts an app. What is the problem?
I need what on second tap app would request permissions again, and so until users agrees with requested permissions. For example Google Chrome and Mozilla Firefox both opens recent files if user denied all permissions, and on next attempt to upload a file ask for permissions again.
Override onRequestPermissionsResult(), check if permission was granted and then call startPictureSelectionActivity(); inside it.
More details in documentation
Added intent on permission denying, now works fine, identical to Mozilla Firefox work.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
if (requestCode == PERMISSION_CAMERA_WITH_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startPictureSelectionActivity();
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, FILECHOOSER_RESULT_CODE);
}
} else if (requestCode == PERMISSION_FINE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationCallback.invoke(locationOrigin, true, true);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Making call in android using button click. No response on button click [duplicate]

How to request a Permission? I tried to documentation, but the constant int request code MY_PERMISSIONS_REQUEST_CALL_PHONE donst seem to just work, anything else to bear in mind for Backwards compatibility?
ActivityCompat.requestPermissions(getApplicationContext(),
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
How to declare MY_PERMISSIONS_REQUEST_CALL_PHONE constant int?
public void makeCall(String s)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + s));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
requestForCallPermission();
} else {
startActivity(intent);
}
}
public void requestForCallPermission()
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE))
{
}
else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall("100");
}
break;
}
}
//Now call the method makeCall("your_desire_phone_numder");
makeCall("100");
Link for more details
For lower versions you need to declare permission in manifest only,
but for marshmellow you need to give it in the code, where you want to execute the code.
Here, you want to make a call. So, insert/include the code provided below in the code block written to make the call.
public void makeCall(){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
if (result == PackageManager.PERMISSION_GRANTED){
startActivity(intent);
} else {
requestForCallPermission();
}
}
private void requestPermission(){
if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)){}
else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall();
}
break;
}
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
startActivity(intent);
Try doing this.
Try below code hope it will help you.
First this will ask you for permission popup after allowing it will call the number.
if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this,
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION);
}
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberToCall));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
} else {
Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show();
return;
}
}
};
You also need to specify the permission you want to use in the AndroidManifest.xml
like
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

onReqestPermissionResult not being called in android dynamic permission

i am trying to invoke the user to enable their write external storage permission at run time. i have used the below code. it shows the run time permission dialog. when i click allow or deny, it will doesn't call the onReqestPermissionResult (call back method) method.
what i have to do for that?
public class PermissionCheck extends Activity {
private static final String TAG = "PermissionCheck";
private Fragment mFragment;
private Activity mActivity;
public PermissionCheck(Fragment fragment){
mFragment = fragment;
mActivity = fragment.getActivity();
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
return true;
} else {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
}
}
}
You are passing another activity's instance in requestPermissions, so the onRequestPermissionResult is called in your mActivity and not in the PermissionCheck. Try changing requestPermissions to this :
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Replace:
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
With :
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
And replace onRequestPermissionsResult() to this:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "Permission is granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
}
break;
}
}
}

onRequestPermissionsResult is not called in fragment android

i am taking runtime permission from user by using below code in fragment .
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
STORAGE_PERMISSION_CODE);
}
and overriding
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 21: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getActivity(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
but override method not called
please give me hint for that i have to override in fragment
Use FragmentCompat instead of ActivityCompat.
Becouse remember for futher u must do this in ACTIVITY not in a fragment. Yoou can d this tricky in fragment but this is bad way
this is in Activity. I check permissions and save it
private static final int REQUEST_CODE_GET_ACCOUNTS = 101;
private static final int REQUEST_AUDIO_PERMISSION = 102;
#TargetApi(23)
public void checkAudioPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
playerFragment.setupVisualizerFxAndUI();
return;
}
if (this.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager
.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
REQUEST_AUDIO_PERMISSION);
} else {
playerFragment.setupVisualizerFxAndUI();
}
}
#TargetApi(23)
public void checkGmailPermission() {
if (isDeviceOnline()) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
showGmailRecFragment(true);
return;
}
if (this.checkSelfPermission(Manifest.permission.GET_ACCOUNTS) != PackageManager
.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.GET_ACCOUNTS},
REQUEST_CODE_GET_ACCOUNTS);
return;
} else {
showGmailRecFragment(true);
}
} else {
Utils.showToast(this, getString(R.string.no_internet));
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_GET_ACCOUNTS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showGmailRecFragment(true);
} else {
Utils.showToast(this, getString(R.string.accounts_permision_denied));
}
break;
case REQUEST_AUDIO_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
playerFragment.setupVisualizerFxAndUI();
} else {
Utils.showToast(this, getString(R.string.audio_permission_denied));
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
and this is on fragment when i want check permission again or if user denied it. Also this save the permissions
private void setupViewVisualizer() {
if (!isLiveTv && !homeVideo.isVideoType()) {
((PlayerActivity) activity).checkAudioPermission();
} else {
return;
}
}
Use this code for Fragment
private void requestReadExternalStorageCameraPermission() {
if (FragmentCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.show(getActivity(), getString(R.string.toast_permission), Toast.ToastType.ALERT);
} else {
FragmentCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE_CAMERA);
}
}
and
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE_GALLARY:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
captureImageInitialization(1);
}
break;
case PERMISSION_REQUEST_CODE_CAMERA:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
captureImageInitialization(0);
}
break;
default:
break;
}
}
Try this..
private static final int REQUEST_RUNTIME_PERMISSION = 123;
if (CheckPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
// you have permission go ahead
} else {
// you do not have permission go request runtime permissions
RequestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Activity context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
I had the same issue,
It is a common mistake and it is very simple to solve.
if you are in an activity we normally use
ActivityCompat.requestPermissions(activity, permissionsNeeded.toTypedArray(),PERMISSION_REQUEST_CODE)
But you shouldn't use the same for a fragment. You can simply use it like requestPermissions(permissionsNeeded.toTypedArray(),PERMISSION_REQUEST_CODE)
If you call the Activity.requestPermission in fragment, the on request callback is get called in the Activity not in the Fragment.
Happy coding
when Request for Permission from fragment
Kotlin
requireActivity().requestPermissions(<Permission array>, <Request code>)
Java
getActivity().requestPermissions(<Permission array>, <Request code>)
You can call fragment onRequestPermissionsResult from activity's onRequestPermissionsResult
Kotlin
var fragments:List? = supportFragmentManager.fragments
var lastFragment:Fragment? = if(fragments!=null && fragments.size>0) fragments.get(fragments.size-1) else null
var lastFragmentName:String? = if(lastFragment!=null) lastFragment.javaClass.name else ""
if (lastFragmentName.equals("com.example.YourFragment")){
lastFragment?.onRequestPermissionsResult(requestCode,permissions,grantResults)
}
Java
List fragments = getSupportFragmentManager().getFragments();
Fragment lastFragment = (fragments!=null && fragments.size()>0)?fragments.get(fragments.size()-1) : null;
String lastFragmentName = (lastFragment!=null) ? lastFragment.getClass().getName() : "";
if (lastFragment!=null && lastFragmentName.equals("com.example.YourFragment")){
lastFragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
To handle permissions in a Fragment call requestPermissions method. If you override onRequestPermissionsResult method in both fragment and activity, containing that fragment, make sure to call super.onRequestPermissionsResult(...) in the activity method to propagate call to the onRequestPermissionsResult method in the fragment.

Android Permission Request Code Issue

How to request a Permission? I tried to documentation, but the constant int request code MY_PERMISSIONS_REQUEST_CALL_PHONE donst seem to just work, anything else to bear in mind for Backwards compatibility?
ActivityCompat.requestPermissions(getApplicationContext(),
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
How to declare MY_PERMISSIONS_REQUEST_CALL_PHONE constant int?
public void makeCall(String s)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + s));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
requestForCallPermission();
} else {
startActivity(intent);
}
}
public void requestForCallPermission()
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE))
{
}
else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall("100");
}
break;
}
}
//Now call the method makeCall("your_desire_phone_numder");
makeCall("100");
Link for more details
For lower versions you need to declare permission in manifest only,
but for marshmellow you need to give it in the code, where you want to execute the code.
Here, you want to make a call. So, insert/include the code provided below in the code block written to make the call.
public void makeCall(){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
if (result == PackageManager.PERMISSION_GRANTED){
startActivity(intent);
} else {
requestForCallPermission();
}
}
private void requestPermission(){
if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)){}
else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall();
}
break;
}
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
startActivity(intent);
Try doing this.
Try below code hope it will help you.
First this will ask you for permission popup after allowing it will call the number.
if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this,
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION);
}
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberToCall));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
} else {
Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show();
return;
}
}
};
You also need to specify the permission you want to use in the AndroidManifest.xml
like
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

Categories

Resources