Search code examples
androidalarmmanagerandroid-6.0-marshmallowandroid-doze-and-standby

Doze and Standby mode for VoIP app


I have an legacy VoIP app for which I want to provide Android's new doze and standby mode support. I have my own messaging/signaling mechanism for which I can't use Android's GCM feature. The documentation stated that white-listing the app will permit to use own signaling mechanism and keep the app alive in Doze mode.

Also I am generating keepAlive alarm using setExtract() and setRepeating() currently to keep alive the persistent connection of XMPP. If I add setExactAndAllowWhileIdle for newer version, its stated that - the Alarm will be triggered at most one in every 15 minutes. But I need to generate it 1 in every 12 seconds interval. I went through Android documentation, many threads in SO and found an article on it. It seems what I want to achieve is not completely possible by white-listing the app and using AlarmManager's new APIs.

What can I do to keep my functionality as like before? Keeping the app alive in doze & standby mode, generating alarm alert with 12 seconds interval to keep the connection alive and keep the network connection open?


Solution

  • White-listing the app by disabling battery optimization is keeping the app alive all time.

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            Intent intent = new Intent();
            intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
        }
    }
    

    Android Manifest:

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

    Although the new AlarmManager API setExactAndAllowWhileIdle is not get called within twelve seconds interval as required. The solution is - we are using JNI codes from before and we are sending keep alive by using a Timer thread to send keep alive alert in twelve seconds intervals. As the app is alive, the timer thread will be alive regardless the Alarm is working or not.

    My app has a very complicated requirements for which I couldn't use GCM high priority message. But most of the VoIP apps like Skype don't disable battery optimization rather use GCM to send notification/messages in doze mode.