如何单独启动Android服务?

时间:2014-04-09 11:44:03

标签: android android-service locationmanager android-4.4-kitkat

在这里我需要什么,如何单独启动Android服务? 我从后台运行服务(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();
}

并启动从BroadcastReceiver运行后运行的位置管理器,但为什么之前的服务消失了,只有Lcation Manager离开?

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) {
}

}

1 个答案:

答案 0 :(得分:0)

问题解决了,我不应该把函数放在 onCreate() 服务上,转移到 onStartCommand() 并且它的工作非常好......