Detecting incoming and outgoing calls in android API29+
Asked Answered
A

2

7

There are many questions about detecting incoming and outgoing calls in android, but all of them are old and also android deprecates the useful functions, and google play rejects my application because I'm using them.

For detecting outgoing calls I used:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

and also listening to android.intent.action.NEW_OUTGOING_CALL broadcast listener.

According to the google document(this) we should use CallRedirectionService and CallScreeningService. but these services work on API 29+.

So is there any way to detect Incoming and outgoing calls that don't deprecate and don't use sensitive permissions?

I want to close my socket connection if there are any calls and reopen it if not.

Aldrin answered 30/6, 2020 at 8:53 Comment(1)
As per me i don't think there is a way to detect calls without sensitive permissions. I recently developed app and i archived the same via android.permission.PROCESS_OUTGOING_CALLS permission but at the end google rejected my app due to using android.permission.PROCESS_OUTGOING_CALLS more sensitive permission. They want strong reason to use such permission.Simons
C
8

You need a BroadcastReceiver for ACTION_PHONE_STATE_CHANGED This will call your received whenever the phone-state changes from idle,ringing,offhook so from the previous value and the new value you can detect if this is an incoming / outgoing call.

A required permission would be "android.permission.READ_PHONE_STATE"

But if you also want to receive the EXTRA_INCOMING_NUMBER in that broadcast, you'll need another permission: "android.permission.READ_CALL_LOG"

here's the receiver definition for your manifest:

<receiver android:name="PhoneStateReceiver"> 
     <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE"/>    
     </intent-filter>
</receiver>

And the code something like this:

public class PhoneStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ...
    }
}
Chic answered 30/6, 2020 at 10:40 Comment(3)
What about out going calls?Aldrin
this code detects both incoming and outgoing, if the state changes idle => ringing you know it's an incoming call, if the state changes idle => offhook, you know it's an outgoing callChic
@Chic I'm not able to get this BroadcastReceiver in Android 13 is there any way to detect incoming and outgoing call above android 13 or can we detect or it's removed from 13 as suspicious permission?Environ
A
5

@marmor answer is correct, I just want to complete it.

for reading phone state first we should add the permission to the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

and in receiver class, we can get current state by reading intent like this:

intent.extras["state"]

the result of extras could be:

RINGING -> If your phone is ringing

OFFHOOK -> If you are talking with someone (Incoming or Outcoming call)

IDLE -> if call ended (Incoming or Outcoming call)

With PHONE_STATE broadcast we don't need to use PROCESS_OUTGOING_CALLS permission or deprecated NEW_OUTGOING_CALL action.

Aldrin answered 30/6, 2020 at 13:7 Comment(1)
for every outgoing call first state I received is OFFHOOK and if the opponent person pick up my call that time I didn't get any state I'll get IDLE when opponent person hang up my call. it is right or is there any way to get any state when we calling someone. 1)) for outgoing call senario OFFHOOK->IDLE. 2)) for incoming call senario RINGING->OFFHOOK->IDLE (if pick-up the cal) RINGING->IDLE(if decline call)Environ

© 2022 - 2024 — McMap. All rights reserved.