1

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();

        }
    }
Charuක
  • 12,953
  • 5
  • 50
  • 88

2 Answers2

1

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).

ʌɐɥqıɐʌ
  • 137
  • 16
  • Thanks for response but i have given the permission to app to start in background. even then it is not working – Rinku kataria Dec 27 '16 at 07:54
  • In normal when service is closed by android OS it is restarted if given, but in MI devices service does not start once closed automatically(instead of permission). You can read it on http://stackoverflow.com/questions/31355084/how-to-keep-my-app-service-keep-running-even-if-app-is-closed-from-task-manger-i – ʌɐɥqıɐʌ Dec 27 '16 at 08:08
1

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

Ciril
  • 420
  • 3
  • 9