Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incoming call listener sleeps after couple of hours

For the last couple of weeks I am facing an issue with telephony manager API in Android - listener for incoming call based on listener starting Recording and on end call stopping recording (The process working smooth)

ISSUE
The issue I am facing is that in some mobile handsets it is working all the time, but in some handsets, Broadcast listener of telephony manager stops working after a few hours. After some research I found a solution that use wake-lock for preventing the CPU to sleep and I tried this but in vain.

   @Override
public void onReceive(Context context, Intent intent) {
//We listen to two intents.  The new outgoing call only tells us of an 
 //outgoing  call.  We use it to get the number.
    roPlantPrefs = RoPlantPrefs.getInstance(context);
    databaseHelper = new DatabaseHelper(context);
     //lastState = roPlantPrefs.getLastState();

    if (roPlantPrefs.getLogin()) {


        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        } else {

            roPlantPrefs = RoPlantPrefs.getInstance(context);
 //            if (!roPlantPrefs.getIsOnCall()) {

            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            int state = 0;
            if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                state = TelephonyManager.CALL_STATE_IDLE;
            } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                state = TelephonyManager.CALL_STATE_RINGING;
            }


            onCallStateChanged(context, state, number);
        }
    }


   //        }
}      

I have also used timer and alarm manger but it is working maximum 2 to 3 hours then listener stops working, Any help could be appreciated.

like image 923
Jhaman Das Avatar asked May 24 '18 07:05

Jhaman Das


1 Answers

I had same issue with Oppo, Vivo, Mi and etc phones, after removing from recent applications app was getting killed even services was getting killed

Solution: I had add autostart permissions like this in my application and it worked.

After resolving this issue my app was getting frozen/killed after some time running in the background due to DOZE mode

Solution: for this condition, just go to ->Settings ->Battery Option, and allow your app to run in the background, if you do this, DOZE mode wont affect on your app,

Cheers

like image 87
Amin Pinjari Avatar answered Nov 05 '22 15:11

Amin Pinjari