Android Permission Request Code Issue - android

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>

Related

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>

Not able to get Android run time permission result

I want to check SMS reading permission is Granted or not in API 23+. So I implemented its as follows;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
Handling permission resp. as follows;
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_SMS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Doing task regarding SMS permission.
}else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) {
//Show an explanation to the user *asynchronously*
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This permission is important to Read SMS.")
.setTitle("Important permission required");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ActivityCompat.requestPermissions(GenerateOTPActivity.this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
});
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}else{
//Never ask again and handle your app without permission.
}
}
}
}
By default SMS permission is Granted so checkSelfPermission() reterns zero but when I manually deny permission from device setting then also checkSelfPermission() returns zero value.
I dont understand how to check SMS permission is denied or not. Please provide me some solutions.
try this codes and also provide permissions in android manifest
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
marshmallowPermission(); //***** marshmaloow runtime permission*****//
}
public Boolean marshmallowPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(this,"Permission already granted.", Toast.LENGTH_SHORT).show();
} else {
requestPermission();
}
}
return true;
}
#TargetApi(Build.VERSION_CODES.M)
public boolean checkPermission(){
int sms = checkSelfPermission(android.Manifest.permission.READ_SMS);
if (sms == PackageManager.PERMISSION_GRANTED ){
return true;
} else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void requestPermission(){
if (shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){
if(shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){
Toast.makeText(this,"sms Permission must be needed which allows to access sms . Please allow sms in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
}
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", this.getPackageName(), null);
intent.setData(uri);
this.startActivity(intent);
} else {
requestPermissions(new String[]{android.Manifest.permission.READ_SMS},100);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permission Granted, Now you can access app without bug.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"Permission Denied, App maybe get crashed.", Toast.LENGTH_LONG).show();
}
break;
}
}

Runtime Permission in Redmi Phone

I am trying to send sms targeted version 24 with runtime permission. It gives SecurityException in Redmi Mi 3s device but working fine on other device with Marshmallow OS.
final int REQ_CODE = 100;
void requestPermission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
CTLogs.printLogs( "Permission is not granted, requesting");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS}, REQ_CODE);
// button.setEnabled(false);
} else {
CTLogs.printLogs("Permission has been granted");
sendSMS();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQ_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CTLogs.printLogs("Permission has been granted");
sendSMS();
} else {
CTLogs.printLogs("Permission denied !!!");
}
}
}
Try this:
// The permission required by the application to work properly
protected static final String[] requiredPermissions;
private static final int PERMISSION_REQUEST = 0;
static {
List<String> perms = new ArrayList<>(Arrays.asList(
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS
));
requiredPermissions = perms.toArray(new String[perms.size()]);
}
Call verifyPermissions() method in onCreate():
private void verifyPermissions() {
if (!hasAllPermissions()) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
requiredPermissions,
PERMISSION_REQUEST
);
}
}
private boolean hasAllPermissions() {
// Check if we have all required permissions.
for (String perm : requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (!hasAllPermissions()) {
finish();
}
return;
}
}
}
once a uninstall app then put this and then install it.
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}

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.

How can I implement CALL intent in android?

This is what I am doing now. I am getting the following error
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.CALL dat=16477210790 }
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(store_contact_no.getText().toString()));
startActivity(callIntent);
}
You don't need to declare dial intent-filter in manifest and don't need any permissions to ACTION_DIAL. Look for my implementation
private void startDialActivity(String phone){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);}
also is good to check is telephony supported on device
private boolean isTelephonyEnabled(){
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
return tm != null && tm.getSimState()==TelephonyManager.SIM_STATE_READY;}
use this code.
this may help you.
Add this to your manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
In Activity check permission,
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
}
} else {
Call(sNumber);
}
where sNumber is your number in string
then Override onRequestPermissionsResult,
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted,
Call(sNumber);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
}
}
break;
}
}
add this method,
private void Call(String sNumber)
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + sNumber));
startActivity(callIntent);
}

Categories

Resources