How do I get the dialer to open with phone number displayed? - android

I don't need to call the phone number, I just need the dialer to open with the phone number already displayed. What Intent should I use to achieve this?

Two ways to achieve it.
1) Need to start the dialer via code, without user interaction.
You need Action_Dial,
use below code it will open Dialer with number specified
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
The 'tel:' prefix is required, otherwhise the following exception will be thrown:
java.lang.IllegalStateException: Could not execute method of the activity.
Action_Dial doesn't require any permission.
If you want to initiate the call directly without user's interaction , You can use action Intent.ACTION_CALL. In this case, you must add the following permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
2) Need user to click on Phone_Number string and start the call.
android:autoLink="phone"
You need to use TextView with below property.
android:autoLink="phone"
android:linksClickable="true" a textView property
You don't need to use intent or to get permission via this way.

Pretty late on the answer, but if you have a TextView that you're showing the phone number in, then you don't need to deal with intents at all, you can just use the XML attribute android:autoLink="phone" and the OS will automatically initiate an ACTION_DIAL Intent.

Okay, it is going to be extremely late answer to this question. But here is just one sample if you want to do it in Kotlin.
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:<number>")
startActivity(intent)
Thought it might help someone.

As #ashishduh mentioned above, using android:autoLink="phone is also a good solution. But this option comes with one drawback, it doesn't work with all phone number lengths. For instance, a phone number of 11 numbers won't work with this option. The solution is to prefix your phone numbers with the country code.
Example:
08034448845 won't work
but +2348034448845 will

<TextView
android:id="#+id/phoneNumber"
android:autoLink="phone"
android:linksClickable="true"
android:text="+91 22 2222 2222"
/>
This is how you can open EditText label assigned number on dialer directly.

You can call Intent following this:
String number = "0123456789";
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);

A helper function for someone using kotlin:
fun openDialPad(context: Context, phoneNum: String) {
val intent = Intent(Intent.ACTION_DIAL)
intent.setData(Uri.parse("tel:$phoneNum"))
context.startActivity(intent)
}

Related

Is there a means to forcefully autodial special numbers on Android?

I've been working on an android app concept in which the app has to auto-dial some special USSD codes in order to initiate certain telco services of interest to the user when the user initiates the service via a shortcut in the app.
The trouble I'm finding is that when the app tries to auto-dial such short codes or USSD numbers, the phone's OS (or is it the Call Intent), doesn't auto-dial, but instead presents the user with the code/number in the dial-pad and so the user has to manually initiate the call - which sort of defeats my intention of allowing users to initiate the services with just one click - the shortcut.
Currently, this is how I'm initiating these calls:
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + number.trim()));
try {
activity.startActivity(intent);
} catch (Exception e) {
Log.d(Tag, e.getMessage());
}
Interestingly, a number such as +256772777000 will auto-dial, launching the user into the call automatically, but a number/code such as 911, *112#, *1*23#, etc won't.
So, what do I need to do differently, or is this not possible at all?
UPDATE
Actually, looking at another app in which I was autodialling user-specified numbers, the problem with the above code trying to auto-dial ussd codes was that instead of using intent.ACTION_CALL, I was using intent.ACTION_DIAL - which definitely just prompts the user with the number to call, without directly calling it. When I fixed that, the app now works as expected. See answer below...
Code samples are most welcome.
Actually, despite what some people were claiming about Android preventing such a feature. When I looked at the code in one of my older apps which auto-dials user-specified numbers, I found the solution to be:
intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number.trim()));
try {
activity.startActivity(intent);
} catch (Exception e) {
Log.d(Tag, e.getMessage());
}
This works as expected - USSD codes get auto-dialled when above code runs. The only important thing to note when using this approach, being that you have to add the following permissions to your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
So, as indicated in the update to my question, the problem with my original approach was using intent.ACTION_DIAL instead of intent.ACTION_CALL.

How can I call emergency number programmatically

I can call general phone number by following codes:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(intent);
in manifest file, I add permission like below:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED"/>
as android's doc explain CALL_PRIVILEGED like this:Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed.Not for use by third-party applications.
And my app is located at packages/apps, so it is not a third-party application, right? So, why I still go straight to dial pad not the dialing user interface every time I call emergency number like '112'?
The code provided should already work for this, but you need the CALL_PHONE and CALL_PRIVILEGED permission to dial emergency numbers without showing the dial-pad.
Android Reference - Manifest Permission CALL_PRIVILEGED
Once that is added to the manifest, you should be able to use the same code using the ACTION_CALL instead to direct dial:
Uri callUri = Uri.parse("tel://911");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);
Try this
If you use Action_Dial then it will open the dialpad directly and Action_Call will call directly on the specified number.
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:" + "198"));
startActivity(intent);
Add permission <uses-permission android:name="android.permission.CALL_PHONE" />
Note: Tested on
Lenovo K3 Note(Marshmallow)
Motorola (Lollipop)
Micromax A116 (Kitkat 4.1.2)
I am also facing similar kind of issue in Android OS 10(Q),
W/Telecom: NewOutgoingCallIntentBroadcaster: Cannot call potential emergency number 911 with CALL Intent Intent { act=android.intent.action.CALL dat=tel:xxx flg=0x10000000 pkg=com.android.server.telecom cmp=com.android.server.telecom/.components.UserCallActivity (has extras) } unless caller is system or default dialer.: TSI.hCI->CIP.sNOCI#AO8
In that case below is my findings and fix. I hope this will help some one.
Intent intent;
if (PhoneUtil.isEmergencyNumber(number)) {
intent = new Intent("com.android.phone.EmergencyDialer.DIAL");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setData(Uri.parse("tel:Your Emergency number "));
} else {
intent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.server.telecom");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:Your number"));
}
startActivity(intent);
Use Action_Dial because Action_Dial will open the dialpad directly and Action_Call will call directly on the specified number.
Note: Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL.
You need the CALL_PHONE and CALL_PRIVILEGED permission to dial emergency numbers without showing the dial-pad.
Once you have added the permissions, try the below code using the ACTION_CALL instead to direct dial:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
Android Reference - Manifest Permission CALL_PRIVILEGED
Quoting the Android Documentation for the permission android.permission.CALL_PRIVILEGED:
Allows an application to call any phone number, including emergency
numbers, without going through the Dialer user interface for the user
to confirm the call being placed.
Not for use by third-party applications.
Constant Value: "android.permission.CALL_PRIVILEGED"
The important part: Not for use by third-party applications.
Therefore, it is not possible using a non-rooted Android OS.
If it were rooted however, you could make your app into a system application and this would work.
Source: https://developer.android.com/reference/android/Manifest.permission.html#CALL_PRIVILEGED
Just to give you a heads up, it has become standard especially in Europe for all the emergency number to be automatically routed to: 112. To change this to the different emergency numbers in the world, (since the US for example uses 911) I would use the locale settings to get the current region and then call the emergency number specific to that region.
This can be easily achieved through an if statement.
Then use the code below to implement the calling part:
Uri callUri = Uri.parse("tel://emergencyNum");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);
As you mentioned, don't forget to add the permissions to the manifest file, otherwise the app will crash:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED"/>
Hope this helps :)
Emergency numbers require the user to fire the action himself.
Dialing 911 (US) is a mistake who could generate fees.
I worked around a project who calls 911 when a user falls. Obviously, it was for elderlies and/or injured people. When he falls, the emergency number is called.
You now have two options to simulate the "human action" as a basic AI.
1) Perform a click on the call button once dialed
Look at Phone app Android Samples and set an alarm which perform a clic on the call button. The sample will give you the button id.
2) Ask for permission (and the user HAS to allow it) following the API 23 modal : https://developer.android.com/training/permissions/requesting.html
The manifest isn't enough in this case.
I've tested a thing which works...
You have to detect if the phone number is an emergency number (they are encoded in the SIM card), then add a '+' at the end of the number if so. I tested with other signs or numbers, it didn't work (the call is done, but it reached not the service).
I put her my code from Orasi (a software for visually impaired people):
// Lance un appel
public static void CallNumber(Activity activity, String szNumber) {
Intent intent = new Intent(Intent.ACTION_CALL);
if(PhoneNumberUtils.isEmergencyNumber(szNumber)) {
szNumber += "+";
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setData(GetUriForCall(szNumber));
activity.startActivity(intent);
}
GetUriForCall encodes the signs. I put also the code here:
// Retourne une URI pour appel téléphonique, avec adaptation encodage caratères
public static Uri GetUriForCall(String szNumber) {
String szOutNumber = szNumber.replace("*", Uri.encode("*")).replace("#",Uri.encode("#"));
return Uri.parse("tel:" + szOutNumber);
}
I'm in France, and I've tested with the '112', which is seen as an emergency number. It works !
Note that the usual emergencies numbers, which are 15 (SAMU), 18 (Fire Department) and 17 (Police) are not seen as emergencies numbers by the SIM card, and work as normal numbers. Moreover, 911 is seen as one, even if you can't call someone with this number here...
So I think it's important to test the number before adding a '+'.
Note also I encode '*' and '#', not '+', so it may explain why this is not working with these signs.
Hope this helps
No permission required in manifest. Call emergency dialer intent:
try {
Intent callIntent = new Intent("com.android.phone.EmergencyDialer.DIAL");
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_NO_USER_ACTION);
callIntent.setData(Uri.parse("tel:112")); // < Optional, will prompt number for user
startActivity(callIntent);
} catch (Exception e) {
// in case something went wrong ...
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
callIntent.setData(Uri.parse("tel:" + any number));
startActivity(callIntent);
permission need the CALL_PHONE and CALL_PRIVILEGED is needed for calling.
It works.
Please read the source code line 212-219 of Android framework:
https://android.googlesource.com/platform/packages/services/Telecomm/+/ec2a7d46128888f025dc3248d083043b978a46f4/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
<uses-permission android:name="android.permission.CALL_PRIVILEGED"/> is only granted to system apps. You need to install your app as system app. Here is a decent tutorial to do that.
try this following code you'll get your answer
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:112");
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getContext(), "Can't make a call", Toast.LENGTH_SHORT).show();
}
and for permission you can give
<uses-permission android:name="android.permission.CALL_PHONE" />

Dial one show another phone number in Android Phone application

searched for a solution but haven't found anything even similar.
When dialing a number from the Android Phone application, based on a prefix predefined earlier, I intercept a call. Afterwards I place another call which number is some access number + 2 sec pause + the number intercepted + #-sign.
My question is - is there any possibility (any ACTION_CALL intent extra or something) that this intercepted and not the access number is shown in the Android Phone application?
Example:
Access Number: 999999
Prefix: 011
Number: 011552657659
Thus, newly placed call is 999999,011552657659#.
Result: the Phone application shows 999999 and other digits are dialed tonally, while I want a user to see something 011552657659 instead.
My code is:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:999999,011552657659#");
context.startActivity(intent);
Thank you in advance!

Not able to make a call when #31# is added

I am trying to implement calling facility from my app. When some condition is true, I am hiding my caller id by adding #31# before my number. Although if #31# is not added, call is made perfectly allright but when #31# is added, blank screen pops up, as if the Phone app is loading, and my application screen comes back. My code snippet is this
String ph = "123456789";
String phNumber;
if (hideCallerId) {
phNumber = "#31#" + ph;
}
else {
phNumber = ph;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phNumber));
startActivity(intent);
Case 1: when phNumber is 123456789, then call is made without problem.
Case 2: when phNumber is #31#123456789, then blank screen pops up and user comes back to my application screen.
Although when I am dialing from my device using #31#123456789, call is successfully made.
What I am missing out. Can anybody help me out.
Links didn't helped me Link1.
Thanks in advance.
This isn't really an answer but my rep isn't high enough for a comment. Is there any chance you have phnumber declared as an int or anything? Maybe the #'s are messing it up?
If that doesn't work have you tried using ACTION_DIAL? the android developer site has more info on the differences between them here. Hope it helps.
You simply have to encode the phone number.
String phonenumber = "03012345,1234,#31#,98765"; // , = pauses
encodedPhonenumber = URLEncoder.encode(phonenumber, "UTF-8");
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + encodedPhonenumber)));
You may want to have a look at this one. Not able to make a call when #31# is added
You need to encode the '#' because it behaves like a dial command, then if you set your action call intent data with a number starting with '#', it will instead call a blank number, trying to dial a number like 8488449#848 if not encoded will dial 8488449 that's why you need to encode at least the '#' symbol with Uri.encode("#")
for example to dial 8488449#848, i will do the following:
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:8488449"+Uri.encode("#")+"848"));

Android How to make a call that includes "#" or "p" in the dial?

I need to make calls in my Android app that includes "#" or "p" in the dial.
If I use the next code:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:629941945#22412"));
startActivity(intent);
It makes the call to the number 629941945 without the # and 22412.
And if I use the next code:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:629941945p22412"));
startActivity(intent);
It makes the call without the "p": 62994194522412.
There is a form that I can solve it?
Intent shortcutIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+Uri.encode("*111*2#")));
startActivity(shortcutIntent);
Try tel:629941945%2322412 instead of tel:629941945#22412
%23 will be replaced with #
Try to use others symbol for pause. Look at http://androidforums.com/android-applications/6733-how-do-i-dial-extension.html or http://code.google.com/p/android/issues/detail?id=7514
But! As I see threre are not one solution and this is depends of phone model
Pause is not supported in the tel URI spec.
http://www.ietf.org/rfc/rfc3966.txt
As stated in another answer %23 can be used to substitute for #.
Some phones don’t have “p” character, but “,” (comma), somewhere it is
“T” and if your phone does not have these fancy characters available,
try “*” or “#“. Hopefully one of this characters will work. Beware, on
some phones are letters case sensitive, so P can not work.
http://rekrowten.wordpress.com/2011/08/29/special-characters-in-telephone-number/
You can not parse such special numbers with an URI since these are not compliant to tel: standard. (http://www.ietf.org/rfc/rfc3966.txt Page 6ff)
You might want to use ACTION_DIAL and give the user the possibility to enter the number himself. Maybe then the phone interprets this as the pause signal p is intended for. To send such pause signals, check Alex Klimashevsky's answer.
Try using "," (comma) instead of p. No idea about "#" though, sorry.

Categories

Resources