Android服务无法启动

时间:2016-05-15 11:23:34

标签: java android service

我有调用服务的活动(主要活动)。该服务需要在后台运行并查找附近的位置。

问题是该服务没有做任何事情(它没有输入onStartCommand)。

如果你能帮助我理解并修复它,我会很高兴。

主要致电服务 -

startService(new Intent(this, BackgroundProcess.class));

服务 -

public class BackgroundProcess extends Service {

private static final String GOOGLE_API_KEY = My_Key;
GoogleMap googleMap;
double latitude = 0;
double longitude = 0;
private int PROXIMITY_RADIUS = 5000; // meter

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


public int onStartCommand(Intent intent, int flags, int startId, Location location) {
    // Let it continue running until it is stopped.
    Log.d("Debag", "Hi");
    //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        Log.d("Debag", "GoolglePlayServices not available");
    }

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    Log.d("Debag", "Hi");
    if (ActivityCompat.checkSelfPermission(BackgroundProcess.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(BackgroundProcess.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return 0;
    }
    location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    Log.d("Debag", "Hi");

    locationManager.requestLocationUpdates(bestProvider, 20000, 0, (LocationListener) this);

    // TODO Alloway check for the locations
    // TODO TIME LOOP
    Log.d("Debag", "Hi");
    String type = "shop"; // TODO place kind
    StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    googlePlacesUrl.append("location=" + latitude + "," + longitude);
    googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
    googlePlacesUrl.append("&types=" + type);
    googlePlacesUrl.append("&sensor=true");
    googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);

    GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask();
    Object[] toPass = new Object[2];
    toPass[0] = googleMap;
    toPass[1] = googlePlacesUrl.toString();
    googlePlacesReadTask.execute(toPass);

    //SystemClock.sleep(30000); // 30 sec wait

    return START_STICKY;
}


private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(BackgroundProcess.this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, null, 0).show();
        return false;
    }
}

public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}
}

1 个答案:

答案 0 :(得分:1)

您必须覆盖Service.onStartCommand(Intent, int, int),但不知何故,您只实施了一个方法onStartCommand(Intent, int, int, Location),该方法不属于服务的生命周期,因此从不自动调用。您应该将Location location声明为局部变量而不是参数,并使用onStartCommand()注释@Override,以便在签名不匹配时收到警告。