Detect Incoming Phone Calls programmatically in Android > 7.0 and Immediate Server Request after that if the Device is in Doze Mode

Viewed 236

I am developing a small Android app, where the app detects phone calls (Incoming, Missed and Outgoing) and immediately sends a phone number to server along with some other values. The app is working in general, but there is a critical restriction. As the device comes into the Doze Mode, this functionality isn't working properly anymore.

I made heavy researches about this problem and haven't found a solution, so, I would be happy if you could help me to find the right one, if it will be possible, or reconfirm, that it is not!

So, I tested two ways of detecting phone calls: Registering a Broadcast Receiver in Activity.

A code snippet is here (registering with Application Context):

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.PHONE_STATE");
intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL"); 
App.getMyAppContext().registerReceiver(callReceiver, intentFilter);

In this case it is working good during the time the Ap is active and shortly after closing it. After some time it is not reacting anymore

With registration a Broadcast Receiver in manifest:

<receiver android:name=".TestPhoneCallReceiver" android:exported="true" >
  <intent-filter>
     <action android:name="android.intent.action.PHONE_STATE" /> 
     <action android:name="android.intent.action.ACTION_PHONE_STATE_CHANGED" />
     <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
  </intent-filter>
</receiver>

The app has also the following permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/>
<uses-permission android:name="android.permissions.READ_PHONE_NUMBERS"/>

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

In this case I am getting phone number during the incoming phone call, if the app is active, but as soon the Device goes to Doze Mode the receiver for "PHONE_STATE" don't give the phone number anymore.

This description is actually described in Android documentation, but I wanted to show, that up to this part the app is tested already.

I presume, that as the device is in Doze Mode, the app can't can't be woken up. So the code below will be presumably ignored:

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

And also the Wi-Fi connection is not available at that time.

So, is it possible to send the incoming phone number immediately after receiving the phone call to server?

It is not about sending the number with some time lag. E.g. check local Android's database periodically and send phone numbers. It is important to send number directly after getting a call.

Thank you for help!

0 Answers
Related