Make telephone call on a button click in android - android

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.

Related

Runtime Permissions Dialog box not showing up in Android 9 and below

The permission dialog does not show up on Android 9 and below. My code works fine on Android 11. i've tried ActivityCompat.requestPermissions() and ActivityResultLauncher but none of them work.
When it runs the line permissionResultLauncher.launch() my app crashes and it also crashes the emulator, such that i have to delete the Emulator and reinstall a new one, otherwise it won't show me anything on the screen.
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
...
</application>
public class MainActivity extends AppCompatActivity{
private ActivityMainBinding binding;
private long pressedTime;
private ActivityResultLauncher<String[]> permissionResultLauncher;
private boolean isRecordPermGranted = false;
private boolean isWritePermGranted = false;
private int totalCount;
private DBHelper dbHelper;
private ImageButton imgbtnRecord, imgbtnList;
private ImageView ivRestore;
private TextView tvLoading, tvRecord;
private boolean asyncTaskRunning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
if(totalCount < 4){
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
}
dbHelper = new DBHelper(this);
asyncTaskRunning = false;
imgbtnRecord = (ImageButton) findViewById(R.id.imgbtnRecord);
imgbtnList = (ImageButton) findViewById(R.id.imgbtnList);
tvLoading = findViewById(R.id.tvLoading);
tvRecord = findViewById(R.id.tvRecord);
ivRestore = findViewById(R.id.ivRestore);
tvLoading.setVisibility(View.GONE);
ivRestore.setVisibility(View.GONE);
imgbtnRecord.setVisibility(View.VISIBLE);
imgbtnList.setVisibility(View.VISIBLE);
tvRecord.setVisibility(View.VISIBLE);
isRecordPermGranted = ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
isWritePermGranted = ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
permissionResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
#Override
public void onActivityResult(Map<String, Boolean> result) {
if(result.get(Manifest.permission.RECORD_AUDIO)!= null){
isRecordPermGranted = result.get(Manifest.permission.RECORD_AUDIO);
Log.d("3","-------------------------" + isRecordPermGranted);
}
if(result.get(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= null){
isWritePermGranted = result.get(Manifest.permission.WRITE_EXTERNAL_STORAGE);
Log.d("4","-------------------------" + isWritePermGranted);
}
if(isWritePermGranted == false || isRecordPermGranted == false){
if(totalCount <3){
finishAffinity();
}else{
Intent intent = new Intent( MainActivity.this, PermissionActivity.class);
startActivity(intent);
}
}
}
});
imgbtnRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isRecordPermGranted == false){
requestPermission();
}else{
openRecordingActivity();
}
}
});
imgbtnList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isRecordPermGranted || !isWritePermGranted){
requestPermission();
}else{
openRecordingsListActivity();
}
}
});
}
public void openRecordingsListActivity() {
Intent intent = new Intent(this, RecordingsListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
public void openRecordingActivity(){
Intent intent = new Intent(this, RecordingActivity.class);
startActivity(intent);
}
#Override
public void onBackPressed() {
if(pressedTime + 2000 > System.currentTimeMillis()){
super.onBackPressed();
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}else{
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
}
pressedTime = System.currentTimeMillis();
}
public void requestPermission(){
List<String> permissionRequests = new ArrayList<>();
if(!isRecordPermGranted && !isWritePermGranted){
permissionRequests.add(Manifest.permission.RECORD_AUDIO);
permissionRequests.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}else if(!isRecordPermGranted){
permissionRequests.add(Manifest.permission.RECORD_AUDIO);
}else if(!isWritePermGranted){
permissionRequests.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
Log.d("permissionRequests list", permissionRequests.toString());
if(!permissionRequests.isEmpty()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
boolean shouldShowRationale = false;
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ){
shouldShowRationale = true;
Log.d("1","-------------------------");
}
if( ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)){
shouldShowRationale = true;
Log.d("2","-------------------------");
}
if(shouldShowRationale){
Toast.makeText(this, "Permissions Needed!", Toast.LENGTH_SHORT).show();
Log.d("5","-------------------------");
}else{
permissionResultLauncher.launch(permissionRequests.toArray(new String[0]));
Log.d("6","-------------------------");
}
}else{
Log.d("old permission ", permissionRequests.toArray(new String[permissionRequests.size()])+" " );
ActivityCompat.requestPermissions(MainActivity.this,permissionRequests.toArray(new String[permissionRequests.size()]), 5);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == 5){
for (int i =0; i< permissions.length; i++){
if(permissions[i].equals(Manifest.permission.RECORD_AUDIO) && grantResults[i] != PackageManager.PERMISSION_GRANTED){
isRecordPermGranted = false;
}
if(permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[i] != PackageManager.PERMISSION_GRANTED){
isWritePermGranted = false;
}
}
}
if(isWritePermGranted == false || isRecordPermGranted == false){
Log.d("REQUEST PERMISSIONS RESULT", "onPermissionResult");
if(totalCount <3){
finishAffinity();
}else{
Intent intent = new Intent( MainActivity.this, PermissionActivity.class);
startActivity(intent);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
When i use the statement if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO )
then it runs the first part of the "OR" statement and shows one of the permission dialog but it never shows both, even with an "&&" or my current code in the requestPermission()
i've tried it without shouldShowRequestPermissionRationale, but it still doesn't work.
I've also added the permissions in the AndroidManifest file.

Closing the "All files access" dialog when user allows access to all files in android

Presently, in my application, when the user is asked to provide "Access all files" permission, the dialog opens and remains on screen even when the user has allowed permission. Below provided screenshot shows the dialog I am referring to:-
I would like to add something in my code, which closes this dialog and opens target activity screen as soon as the user enables the toggle of "Allow access to all files".
GrantPermissionsActivity :-
public class GrantPermissionsActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
MaterialButton cancelMaterialButton, grantMaterialButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grant_permissions);
findViews();
initViews();
}
private void findViews() {
cancelMaterialButton = findViewById(R.id.materialButtonCancel);
grantMaterialButton = findViewById(R.id.materialButtonGrant);
}
private void initViews() {
if (checkPermission()){
Intent intent = new Intent(GrantPermissionsActivity.this,PasswordActivity.class);
}
cancelMaterialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(GrantPermissionsActivity.this, "Need to give permission!", Toast.LENGTH_SHORT).show();
finish();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
});
grantMaterialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkPermission()) {
Toast.makeText(GrantPermissionsActivity.this, "Permission already granted", Toast.LENGTH_SHORT).show();
} else if (!checkPermission()) {
requestPermission();
}
}
});
}
private boolean checkPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int result = ContextCompat.checkSelfPermission(GrantPermissionsActivity.this, READ_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(GrantPermissionsActivity.this, WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
}
} else {
//below android 11
ActivityCompat.requestPermissions(GrantPermissionsActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// perform action when allow permission success
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.d(TAG, "onActivityResult: Reachedinresult");
if (requestCode == 2296 && resultCode == RESULT_OK) {
Log.d(TAG, "onActivityResult: Reached request code");
if (SDK_INT >= Build.VERSION_CODES.R) {
Log.d(TAG, "onActivityResult: Reached SDKINT");
if (Environment.isExternalStorageManager()) {
// perform action when allow permission success
/*Intent intent = new Intent(GrantPermissionsActivity.this,PasswordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);*/
Log.d(TAG, "onActivityResult: Reached");
} else {
checkPermission();
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Please let me know the changes in the above code.
Also, check the below sample for reference:-
Replace the method requestPermission() to
private void requestPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if(Environment.isExternalStorageManager()) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// perform action when allow permission success in android 11
}
});
this.cancel();
}
}
},2000,1000);
}
} else {
//below android 11
ActivityCompat.requestPermissions(GrantPermissionsActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}}
If you want to start an activity from there, add flag Intent.FLAG_ACTIVITY_NEW_TASK in the Intent object, otherwise it may not work.

Android startactivityforresult not called after permissionrequest

OnActivityResult is not called and I wonder if it's because I just added a permissionrequest (it was working fine before). Below is the code, it's launching an intent with startactivityforresult after having requested a permission.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.v("showEmails","build<");
return true;
}
if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
return true;
}
if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
Snackbar.make(findViewById(android.R.id.content), "permission_rationale", Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
#Override
#TargetApi(Build.VERSION_CODES.M)
public void onClick(View v) {
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
});
} else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
Log.v("showEmails","requestPermissions");
}
return false;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
ImageView ivShare = (ImageView) findViewById(R.id.ivShare);
ivShare.setImageURI(imageUri);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(Intent.EXTRA_TEXT, textIntent);
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
emailIntent.setFlags(0);
startActivityForResult(emailIntent, GMAILSENT);
}
}
}

[Unity 3D]I want access Android Camera and pick up path

I make an AARplugins for android which accesses Camera.
But it does not activate onActivityResult.
public static void showCamera ( ) // Connect Camera
{
String Uri;
Activity CameraActivity = UnityPlayer.currentActivity;
Toast.makeText(CameraActivity , "first" ,Toast.LENGTH_LONG).show();
Log.d(" before ImageUrl###### ", "first");
CameraActivity.runOnUiThread(new Runnable()
{
public void run() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraActivity.startActivityForResult(intent,1);
}
});
}
///////////////Don't activate↓//////////////////////////////////
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d(" before ImageUrl###### ", "twotwotwotwotwotwotwo"); //Don't activate
}
////////////////////////////////////////////////////////////////////////////
why don't activate this?
Try This,
Camera permission
private static final int REQUEST_CAMERA = 112;
btn_cam.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.CAMERA};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_CAMERAE );
} else {
//open camera
}
} else {
//open camera
}
}
});
}
get Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//open camera
}
}
}
}
check permissions for marshmallow
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />

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