androidandroid-servicelocationmanagerandroid-4.4-kitkat

How can I start Android service separately?


Here what I need, how can I start Android service separately? I got service running from background (Preference.Class),

    public void startService(){
    Intent intent;
    intent = new Intent(getBaseContext(), ForegroundService.class);
    startService(intent);

    intent = new Intent(getBaseContext(), PostMobileHistory.class);
    startService(intent);

    intent = new Intent(getBaseContext(), PostLocation.class);
    startService(intent);

    sharedPreferences.edit().putBoolean("serviceStatus", true).commit();
}

and start the Location Manager which run in service after from the BroadcastReceiver, but why the previous service gone, and just only Lcation Manager left?

public class LocationManager extends Service implements LocationListener{

private android.location.LocationManager locationManager;

private String previousProvider = "gps";
private float previousAccuracy = 100;

public String mobileHistory_GUID = null;

Context context = this;

@Override
public void onCreate() {
    SharedPreferences sharedPreferences = context.getSharedPreferences("mobileHistory", Context.MODE_PRIVATE);
    mobileHistory_GUID = sharedPreferences.getString("GUID", "DEFAULT");
    Log.i("SP Mobile History GUID", mobileHistory_GUID);

    startTracking();
}

public void onDestroy(){
    stopTracking();
}

@Override
public IBinder onBind(Intent intent) {

    return null;
}

public void stopTracking(){
    if(locationManager != null){
        locationManager.removeUpdates(this);
        Log.i("Location Manager", "Tracking stopped");
    }
}

public void startTracking() {
        //Get the location manager
        locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
        boolean isNetworkEnabled = locationManager.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            Log.i("Provider Status", "No provider");
        }
        else {
            if (isNetworkEnabled) {
                String locationProvider = android.location.LocationManager.NETWORK_PROVIDER;
                Log.i("Provider Status", "Network enabled");
                // Register the listener with the Location Manager to receive location updates
                locationManager.requestLocationUpdates(locationProvider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }
            if (isGPSEnabled) {
                String locationProvider = android.location.LocationManager.GPS_PROVIDER;
                Log.i("Provider Status", "GPS enabled");
                // Register the listener with the Location Manager to receive location updates
                locationManager.requestLocationUpdates(locationProvider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }
        }

        if(locationManager != null){
            Log.i("Location Manager", "Tracking started");
        }
}

public void onLocationChanged(Location location) {

    SharedPreferences sharedPreferences = context.getSharedPreferences("mobileHistory", Context.MODE_PRIVATE);
    String mobileHistory_GUID = sharedPreferences.getString("GUID", "DEFAULT");

    String location_GUID = String.valueOf(UUID.randomUUID());

    if (location != null) {
        String latitude = valueOf(location.getLatitude());
        String longitude = valueOf(location.getLongitude());
        String altitude = valueOf(location.getAltitude());
        String accuracy = valueOf(location.getAccuracy());
        String speed = valueOf(location.getSpeed());
        String provider = location.getProvider();

        float floatAccuracy = Float.valueOf(accuracy);

        if(floatAccuracy <= 20 && provider.equals("gps")){
            Arrays.setLocationArrayList(new Model_Location(location_GUID, mobileHistory_GUID, provider, latitude, longitude, altitude, accuracy, speed));
            Log.i("Location", "GPS");
            Log.i("Location", "<= 20");
            Log.i("Location", latitude + " " + longitude + " " + altitude + " " + accuracy + " " + speed);
            previousProvider = provider;
            previousAccuracy = floatAccuracy;
        }
        else if(floatAccuracy > 20 && provider.equals("gps")){
            previousProvider = provider;
            previousAccuracy = floatAccuracy;
        }
        else if(floatAccuracy > 20 && floatAccuracy <= 100 && provider.equals("network")){
            if(previousAccuracy > 20 && previousProvider.equals("gps")){
                Arrays.setLocationArrayList(new Model_Location(location_GUID, mobileHistory_GUID, provider, latitude, longitude, altitude, accuracy, speed));
                Log.i("Location", "Network");
                Log.i("Location", ">= 20 && <= 50");
                Log.i("Location", latitude + " " + longitude + " " + altitude + " " + accuracy + " " + speed);
            }
        }
        else{
            Log.i("Location", latitude + " " + longitude + " " + altitude + " " + accuracy + " " + speed);
        }
    }
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}

}


Solution

  • The problem solve, I shouldn't put function in the onCreate() on service, moved to onStartCommand() and its work perfectly well...