Jhaman Das
Jhaman Das

Reputation: 1114

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.

Upvotes: 10

Views: 299

Answers (2)

user7596908
user7596908

Reputation:

try this intent of setting then put your application name (don't optimize) for sdk 23 and up

 Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    startActivity(intent);

Upvotes: 0

Amin Pinjari
Amin Pinjari

Reputation: 2173

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

Upvotes: 1

Related Questions