Location service is not working Mi devices when app is destroyed

1.8k views Asked by At

Here is my code in the service please let me know where i'm missing something. Code is working in other devices problem is only in the redmi devices.

Thanks in advance.

public class AppLocationServices extends Service implements IAsyncTask{

        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }

        @Override
        public int onStartCommand(final Intent intent, final int flags,
                                  final int startId) {
            if(getBaseContext()!=null){
                SessionManagement sessionManagement = new SessionManagement(getBaseContext());
                if(sessionManagement.isLoggedIn() && !Util.IsOwner(Util.GetLocalStorage(sessionManagement))){
                    if(getBaseContext()!=null && intent.getExtras()!=null && intent.getExtras().getString("ClassName").equalsIgnoreCase(ReceiverLocationAlarm.class.getName())){
                        Util.SetOnReceiverLocationIntent(getBaseContext());
                        LocationProvider mLocationProvider = new LocationProvider(getBaseContext());
                        mLocationProvider.connect();
                    }
                    else if(getBaseContext()!=null && intent.getExtras()!=null && intent.getExtras().getString("ClassName").equalsIgnoreCase(SenderLocationAlarm.class.getName())){
                        Util.SetOnSenderLocationIntent(getBaseContext());
                        DataBaseHelper dataBaseHelper = new DataBaseHelper(getBaseContext());
                        if(dataBaseHelper.getFromGpsItem().size()>0)
                            new SendLocationToServer(getBaseContext(),AppLocationServices.this).execute();
                    }
                    }else {
                    LocationProvider mLocationProvider = new LocationProvider(getBaseContext());
                    mLocationProvider.disconnect();
                    Util.StopOnReceiverLocationIntent(getContext());
                    Util.StopOnSendLocationIntent(getContext());
                    stopSelf();
                }
            }
            return Service.START_REDELIVER_INTENT;
        }


        @Override
        public Context getContext() {
            return getBaseContext();
        }

        @Override
        public Fragment getFragment() {
            return null;
        }

        @Override
        public void OnPreExecute() {

        }

        @Override
        public void OnPostExecute(String URL, JSONObject jsonObject) {
        }

        @Override
        public void OnErrorMessage(String Message) {

        }


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

        }
    }
2

There are 2 answers

0
Ciril On BEST ANSWER

This is a specific issue occurred in redmi devices. One way to fix this issue is open the redmi security app, open Permissions tab and enable autostart of your app. Else you can keep running the application. ie start the application automatically when destroyed

2
ʌɐɥqıɐʌ On

In MI devices you can not run a service in background when app is closed(destroyed). There is concept of permission manager in many devices(xiomi,mi etc).