I know it was very simple to do it but I come across a very strange issue. I have to call Police in danger Situation by just tapping a button. So I have used following code to call.
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:100"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(callIntent);
Added CALL_PHONE
permission in AndroidManifest.xml
. The Issue is that it is opening the 100 on Dial Pad but not making call directly. I want that to happen immediately when user clicks on the button.
When I tried to to put +91 before 100 it is calling the number automatically but why plus is required for such numbers. So Someone help me how to solve this issue
From the documentation of ACTION_CALL
:
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.
So it seems this behavior is on purpose.
There could be a problem that the android system doesnt recognize 100
as a valid phone number, instead if you put the country code before it then it works fine. TO solve such issue take a look at this library libnhonenumber. You could use it something like this
public static ArrayList<String> extractPhoneNumber(String content) {
ArrayList<String> numbers = new ArrayList<String>(0);
PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
//Change IT with your contry code
Iterable<PhoneNumberMatch> matches = instance.findNumbers(content, "IT");
Iterator<PhoneNumberMatch> iterator = matches.iterator();
while (iterator.hasNext()) {
numbers.add(instance.format(iterator.next().number(), PhoneNumberFormat.INTERNATIONAL));
}
return numbers;
}
private void phoneCall()
{
String phoneCallUri = "tel:91";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
Best way to directly call without user intervention..
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
There are two intents to call/start calling: ACTION_CALL and ACTION_DIAL.
ACTION_DIAL will only open the dialer with the number filled in, but allows the user to actually call or reject the call. ACTION_CALL will immediately call the number and requires an extra permission.
So make sure you have the permission
A Long time passed. But may help someone else.
If you want to call directly, you should use requestPermissions method.
1. Add this line to your manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
2. Define a class variable in the activity class:
private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
3. Add these lines to the onCreate method of the activity:
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
4. And for making a call immediately after clicking on Allow button, override onRequestPermissionsResult method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
final int permissionsLength = permissions.length;
for (int i = 0; i < permissionsLength; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
startActivity(phoneCallIntent);
}
}
}
When a user give the permission, next time there will be no dialogue box and call will be make directly.