简体   繁体   中英

Background service stops working randomly in android in onCreate() and onResume()

I am working on android application in which i need to start the location service. All i need to make it sure that the service should work, whether it will be on any activity, if i press the back button/home button or even if i sweep the application by pressing home button. My location service stops working after sometime like i put the timing of 1 minute but it will call it after 2-3 minutes.

private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(1000 * 60 * 1) // 30 minutes seconds
            .setFastestInterval(1000 * 60 * 1) // 16ms = 60fps
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

My code is given below for calling location service class and main class where i am running calling that service. Please help me out in the above described scenario where i want to run that service in background: When press the back button, home button, removing application by pressing home button.

public class GPSLoggerService extends Service {

private LocationManager lm;
private static long minTimeMillis = 2000;
private static long minDistanceMeters = 0;
private static float minAccuracyMeters = 35;
private static boolean showingDebugToast = false;
MyLocationTracker locationTracker;
private static final String tag = "MUrgency GPS Logger";


/** Called when the activity is first created. */
private void startLoggerService() {

    if (locationTracker != null)
        return;

    locationTracker = new MyLocationTracker(this) {

        @Override
        public void onLocationFound(Location location) {

            Constants.sMY_LOCATION = location;

            float a = (float) location.getLatitude();
            float b = (float) location.getLongitude();

            SharedPreferences prefs = getSharedPreferences("locationPref", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putFloat("latitudeFloat", a);
            editor.putFloat("longitudeFloat", b);
            editor.commit();

                if (minutes > 5){
                    shouldSync = true;
                }

        }
    };
}

private void shutdownLoggerService() {
}



}

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

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

// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

/**
 * Class for clients to access. Because we know this service always runs in
 * the same process as its clients, we don't need to deal with IPC.
 */
public class LocalBinder extends Binder {
    GPSLoggerService getService() {
        return GPSLoggerService.this;
    }
}

}

Main class where i am calling service at onCreate()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainlanding);
        startService(new Intent(this, GPSLoggerService.class));
}

@Override
    protected void onDestroy() {
        sActivityMain = null;
        super.onDestroy();
        stopLocationService();
    }

From my view I can see this is a normal process, when the app enters OnPause method, this starts to works in background then you need a background process to execute your class and functions that you want.

If this is your first time using parallel programming I think you need to dedicate a little bit of your time to search information about this. It's amazing form to work with background processes. Really it's the difference between a normal android programmer and professional android programmer (among other things) because with the background processes can use all potency of your device.

Tell me if I helped you, good programming!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM