0

I'm trying to make a phone call from my app in a way it does not need to switch activities, But every guide I find has the following code snippet,

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:XXXXXXXXX"));
startActivity(callIntent);

which switches the activity I start the call from to another activity (in this case to an activity in a different app). Is there a way to stop this from happening? I managed to do it with a third party library called "sinch" but I'm wondering if there is a native way to do it or maybe a better library?

Ps- the app I'm building it for myself, basically, I'm building a voice assistant that can make calls via voice commands, hence I can't let it switch activities. I have no intention of publishing it on the app store and I have no difficulty giving dangerous permissions :) My plan is to run it on a separate piece of hardware in the future.

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
  • 2
    Actually, it's quite deliberate that the user should be aware of the phone dialler becoming active to prevent bad applications deliberately calling numbers that charge money to the user without their consent. The same is true for actually making a call automatically. Bringing up the dialer with a number.. sure. Actually placing a call automatically.. that requires a "dangerous" permission which needs user approval. – C B J Feb 01 '21 at 12:25
  • Does this answer help you? https://stackoverflow.com/a/49835987/14759470 – SlothCoding Feb 01 '21 at 12:59
  • @CBJ permissions are not really a problem, I'm actually building a personal app for myself :) can you elaborate a bit on this approach? – Ashen Jayawardena Feb 01 '21 at 13:20
  • 3
    Notice that Google considers the required permissions high risk, and hence only allows apps with this permissions in very special cases. See https://support.google.com/googleplay/android-developer/answer/10208820?hl=en – Xavier Rubio Jansana Feb 01 '21 at 13:21
  • Can you tell us a little more about your application and the specific requirements? It will help to understand the problem you need to solve. – C B J Feb 01 '21 at 13:22
  • @CBJ Hey I added some more context to my question, I'm sorry I didn't do it earlier :') – Ashen Jayawardena Feb 01 '21 at 13:38
  • @XavierRubioJansana added some more context to the question :) – Ashen Jayawardena Feb 01 '21 at 13:41

2 Answers2

0

This link can help you, but as Xavier Rubio Jansana wrote previously, Google hardly accepts applications that do not use intents to make phone calls : https://google-developer-training.github.io/android-developer-phone-sms-course/Lesson%201/1_c_phone_calls.html

Google wants any programmer to use an intent to make the user view the default phone application handle the phone call.

If your app does not need to be available on Google Play Store then it would be ok for you to make the phone call directly.

KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • Well, my question is actually how to do it directly? the link that you have pasted follows the method I want to avoid :( it passes intents and switches applications. – Ashen Jayawardena Feb 01 '21 at 20:47
  • Yes you are right, I know that it was possible with SMSes but for phone calls I think that it's not possible. – KotlinIsland Feb 02 '21 at 08:53
  • Can this page help ? https://developer.android.com/reference/android/telecom/TelecomManager#placeCall(android.net.Uri,%20android.os.Bundle) – KotlinIsland Feb 02 '21 at 08:55
0

To elaborate on what I was talking about earlier, it is talked about in this stack overflow question (perhaps upvote their answers if they are helpful). How to make a phone call using intent in Android?.

From memory, ACTION_DIAL will bring up the dialler and pre-populate the number.. it requires no special permission because it requires the user to actually place the call.

On the other hand, ACTION_CALL will actually initiate the call and requires special permissions.

If returning focus (bringing your app back to the foreground) is a concern, you could try using a scheduled intent to (re) launch your activity (maybe using alarm manager or similar) some time after invoking the dialler. You can retain state with a little care around launch modes in the intent and/or a special "do nothing" activity published in your app manifest which does nothing but re-activate your app.

C B J
  • 1,838
  • 16
  • 22
  • I see, but my question is how to avoid intents :) i dont want my UI to switch when i make the call. – Ashen Jayawardena Feb 02 '21 at 18:17
  • Well, there may be something you can do in the telephony apis, but generally switching UIs is deliberate so the user knows a call is being made. It's a safety feature. Going to effort to place a call without informing the user in any way is usually a suspicious thing to do. I'm sorry, but I'm not inclined to give more detail on this in case someone else abuses the information. If you are looking to develop an external device for this, perhaps consider prototyping with a raspberry pi or arduino and a cellular shield (dev board). – C B J Feb 04 '21 at 04:08