Search code examples
androidandroid-serviceandroid-broadcast

Service and BroadcastReceiver not working in MI phone at time of Memory kill or Destroy Activity


I am Create a one RestartServiceBroadcast to keep my background service always alive after killing the application.

  public class RestartServiceBroadcast extends BroadcastReceiver {

                Context ctx;
                private PreferencesProviderWrapper prefProviderWrapper;

                @Override
                public void onReceive(Context context, Intent intent) {
                    this.ctx= context;
                    System.out.println("RestartServiceBroadcast:");
                    if(intent.getAction().equals(ipManager.INTENT_SERVICE_RESTART)){
                        startOneService();
                    }

                }

                private void startOneService() {


            }
                }

Here, I am also creating one service to connect server IP.in which I also trigger a Broadcast to restart the same service. I am also using START_STICKEY in Service

public class IpService extends Service {

@Override
    public void onCreate() {
        super.onCreate();

    }

@Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("ipStack : onDestroy");

        Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
        sendBroadcast(broadcastIntent);
    }
}

I am calling Broadcast in mainActivity onDestroy() Method.

 override fun onDestroy() {
           val broadcastIntent = Intent(ipManager.INTENT_SERVICE_RESTART)
            sendBroadcast(broadcastIntent)
            super.onDestroy()
        }

This is my Manifest

 <service
            android:name=".service.ipService"
            android:enabled="true"
            android:exported="false"
            android:permission="demo.magic.mobile.android.permission.CONFIGURE_IP"
            android:process=":ipStack"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="demo.magic.mobile.service.ipService" />
                <action android:name="demo.magic.mobile.service.ipConfiguration" />
            </intent-filter>
        </service>




        <receiver
            android:enabled="true"
            android:exported="true"
            android:name=".service.receiver.RestartServiceBroadcast"
            android:process=":ipStack">
            <intent-filter>
                    <action android:name="demo.magic.mobile.service.RestartService" />
            </intent-filter>
        </receiver>

Class ipManager for getting a value of Broadcast

public final class ipManager {

    public static final String INTENT_SIP_SERVICE_RESTART = "demo.magic.mobile.service.RestartService";
}

All code is working fine in my motoG5 and MI Note5 Pro devices. but in the MI note4 and MI4 devices service and broadcast has been killed after removing the app from background.

MotoG5 and other devices Logcat Like this when I am killing the application.

2019-04-25 15:27:00.220 2345-3735/? W/ActivityManager: Scheduling restart of crashed service demo.magic.mobile/.service.ipService in 20942ms
2019-04-25 15:27:21.192 2345-2410/? I/ActivityManager: Start proc 23954:demo.magic.mobile:ipStack/u0a247 for service demo.magic.mobile/.service.ipService
2019-04-25 15:27:21.490 23954-23954/demo.magic.mobile:ipStack I/System.out: ipStack : onstart 

Logcat of MI Note4 kill application process

2019-04-25 15:26:04.584 1604-2918/? I/ActivityManager: Start proc 10971:demo.magic.mobile:ipStack/u0a643 for service demo.magic.mobile/.service.ipService caller=demo.magic.mobile
2019-04-25 15:36:04.216 1604-2785/? I/ActivityManager: Start proc 19393:demo.magic.mobile:ipStack/u0a643 for service dailer.demo.magic.mobile/.service.ipService caller=demo.magic.mobile

Help will be appreciated, Thanks in Advance.


Solution

  • Finally, I got a Perfect Solution. I am testing in oreo with all OS like oxygen, color, and miui. I am developing a VoIP Application and in my application one sip service is continuing running after an app is destroying or killed by the user. one more thing user set app is always running background and battery optimization in the phone setting.

    I am Using Alarm Manager For Keep my service alive it is better than a job scheduler and call service again. I implement this code in service class oncreate() method.

    PendingIntent mKeepAlivePendingIntent;
         public class CallService extends Service {
    
                    @Override
                    public void onCreate() {
                        super.onCreate();
                        Intent intent = new Intent(this, RestartServiceBroadcast.class);
                        mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        ((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, 60000, mKeepAlivePendingIntent);
                    }
    
                }
    

    Create BroadcastReceiver to call service again when user killed an application from memory task

    public class RestartServiceBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("service.RestartService")) {
            // Here Call a Service
        }
    
        }
    }
    

    Manifest Like this

    <receiver
                    android:name=".service.receiver.RestartServiceBroadcast"
                    android:enabled="true"
                    android:exported="true"
                    android:process=":sipStack">
                    <intent-filter>
                        <action android:name="service.RestartService" />
                    </intent-filter>
       </receiver>
    
            <service
                android:name=".service.CallService"
                android:enabled="true"
                android:exported="false"
                android:stopWithTask="false">
    
            </service>