I am trying to make call from my app. But every time it get crashed with no error shown on logcat. I took permission in manifest also check it at runtime.
public void call(String number){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
Are you sure context is not null ? You should do something like this.
Inside your calling activity make these changes
private static final int REQUEST_CALL_PHONE_PERMISSION = 100;
if( isCallPhonePermissionGranted() ){
call("<Number>");
} else {
call("<Number>");
}
private void requestCallPermission() {
final String[] permissions = new String[]{Manifest.permission.CALL_PHONE};
ActivityCompat.requestPermissions(this, permissions, REQUEST_CALL_PHONE_PERMISSION);
}
private boolean isCallPhonePermissionGranted() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[]
permissions, #NonNull int[] grantResults) {
if (requestCode != REQUEST_CALL_PHONE_PERMISSION) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
return;
}
if (grantResults.length != 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
call("<Number>");
return;
}
}
public void call(String number){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
And finally add this this permission to Android Manifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
Don't forget to add the relevant permission to your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
An intent by itself is simply an object that describes something. It doesn't do anything.
public void call(String number){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
context.startActivity(intent);
}
And How to make call in Android 6.0 and Above
Related
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>
I have implemented a telephonic service in my app their is a button when we click on that button it will make a call but the problem is it not supporting all devices on some devices it is working well and in some devices it is not working now provide me any solution what to do so that it work on all devices.
here is my code.
public class SOSCallHelp extends AppCompatActivity {
private Button call1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soscall_help);
call1=(Button)findViewById(R.id.call1);
call1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9078784565"));
if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
{
return;
}
startActivity(callIntent);
}
});
Please make sure you add the below permission in your App Manifest for Pre-Lollipop devices.
<uses-permission android:name="android.permission.CALL_PHONE" />
Request Permission
<uses-permission android:name="android.permission.CALL_PHONE" />
For Calling
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9078784565"));
if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
{
return;
}
startActivity(callIntent);
try this:
final int PERMISSION_REQUEST_CODE = 111;
if (driverMobile != null) {
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+
if (!checkCallPhonePermission() || !checkReadStatePermission()) {
requestPermission();
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + 1234567899));
startActivity(callIntent);
}
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + + 1234567899));
startActivity(callIntent);
}
} else {
}
//permission
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_CODE);
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_CODES);
}
private boolean checkCallPhonePermission() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private boolean checkReadStatePermission() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
#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) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + 123456789
startActivity(callIntent);
}
break;
}
}
Please change to this -
public class SOSCallHelp extends AppCompatActivity {
private Button call1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soscall_help);
call1 = (Button) findViewById(R.id.call1);
call1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9078784565"));
if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
{
return;
}
PackageManager packageManager = getActivity().getPackageManager();
if (callIntent.resolveActivity(packageManager) != null) {
startActivity(callIntent);
} else {
Log.d("SOSCallHelp", "No Intent available to handle this action");
}
}
});
}
}
Simply use :
private static int REQUEST_CALL_PHONE = 111;
private void requestCallPhonePermission() {
String callPermission = android.Manifest.permission.CALL_PHONE;
int hasPermission = ContextCompat.checkSelfPermission(SOSCallHelp.this, callPermission);
String[] permissions = new String[]{callPermission};
if (hasPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, REQUEST_CALL_PHONE);
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:9078784565"));
startActivity(intent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CALL_PHONE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:9078784565"));
startActivity(intent);
} else {
Toast.makeText(SOSCallHelp.this, "CALL_PHONE DENIED", Toast.LENGTH_SHORT).show();
}
}
}
Now call this requestCallPhonePermission() function on button click
In manifest file add <uses-permission android:name="android.permission.CALL_PHONE" />
permission.
From my activity when i call Contacts to get a contact number the application crashed. Application is running ok on other devices but when i'm try to run it in android 6.0, it crashed. i've no idea what i'm doing wrong.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
After getting the the data in onActivityResult method.
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = this.getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Log.d(TAG, "name : "+name+" , Phone Number : "+ phoneNumber);
}
}
break;
}
}
can Anyone help me ?
All of the answers here are wrong. You shouldn't need READ_CONTACT permission at all to retrieve single contact using ACTION_PICK intent. You should be granted this permission temporary to be able to retrieve contact specific data. However there are some devices that don't implement this API in a good way. Myself I have found Sony Xperia Z3, but I heard about HTC devices also had this problem.
To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
Source: https://developer.android.com/guide/components/intents-common.html#Contacts
[UPDATE]
After upgrading Sony Xperia Z3 system image to version 23.5.A.1.291 bug does not occur anymore.
From android 6.0 (Mashmello) , android has introduced Run Time Permissions for users to grand permission to apps while the app is running, not when they install the app. So, Your calling should be like this ->
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/*If Android M*/
if (ContextCompat.checkSelfPermission(SendMoneyByDetailsActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
/*If Android M and Not permission granted */
ActivityCompat.requestPermissions(SendMoneyByDetailsActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
} else {
/*If Android M and permission granted */
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
} else {
/*IF not Android M*/
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
And when user grant the permission then you can call the intent to pick contact list ->
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission was granted, yay! Do the
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
} else {
// permission denied
}
return;
}
}
}
And don't forgot to add permission in your manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
It not your manifest problem its problem with android 6.0 it requires Runtime Permissions...
after adding this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
Just change your code to this In your code..
public class MainActivity extends Activity {
public static final int REQUIRED_PERMISSIONS = 001;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//give here permissions what ever you want...
// if you are using one add only one... not all..
if ((CheckPermission(this, Manifest.permission.READ_CONTACTS))&&
(CheckPermission(this, Manifest.permission.READ_PHONE_STATE))&&
(CheckPermission(this, Manifest.permission.INTERNET))&&
(CheckPermission(this, Manifest.permission.ACCESS_NETWORK_STATE))&&
(CheckPermission(this, Manifest.permission.ACCESS_WIFI_STATE)))
{
PermHandling();
}
//now reqest runtime permissions..
else {
RequestPermission(MainActivity.this, Manifest.permission.READ_CONTACTS, REQUIRED_PERMISSIONS);
RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.INTERNET, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE, REQUIRED_PERMISSIONS );
RequestPermission(MainActivity.this, Manifest.permission.ACCESS_WIFI_STATE, REQUIRED_PERMISSIONS );
}
}
private void PermHandling() {
//Your app internal parts....
//Here your stuff works...
}
//private void NewPermHandling(){
//}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUIRED_PERMISSIONS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermHandling();
} else {
//Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
}
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(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
I also faced same problem my applications worked on till 5.1 but 6.0+ due to Runtime permissions We need to add them.. and accept those permissions at runtime
I think this is permission issue
add bellow permission inside manifest file
<uses-permission android:name="android.permission.READ_CONTACTS" />
accept permissions in marshmallows
https://developer.android.com/training/permissions/requesting.html
actual implementation
add bellow code inside Main Activity
call checkAndAddPermission() method inside onCreate() method
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 1244;
private void checkAndAddPermission() {
List<String> permissionsNeeded = new ArrayList<>();
final List<String> permissionsList = new ArrayList<>();
if (!addPermission(permissionsList, android.Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("ReadContacts");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
}
#TargetApi(Build.VERSION_CODES.M)
private boolean addPermission(List<String> permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initial
perms.put(android.Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
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>
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);
}