简体   繁体   English

Android服务无法启动

[英]Android service not start

I have activity (main activity) that calls to a service. 我有调用服务的活动(主要活动)。 The service needs to run in the background and look for nearby locations. 该服务需要在后台运行并查找附近的位置。

The problem is that the service is not doing anything (it doesn't enter onStartCommand ). 问题在于该服务未执行任何操作(未输入onStartCommand )。

I would be happy if you can help me understand the problem and fix it. 如果您能帮助我理解问题并解决问题,我将非常高兴。

Main call to the Service - 服务的主要电话-

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

Service - 服务-

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

You have to override Service.onStartCommand(Intent, int, int) , but somehow you've only implemented a method onStartCommand(Intent, int, int, Location) which is not part of the Service's life cyle and thus never called automatically. 您必须重写Service.onStartCommand(Intent, int, int) ,但是不知何故,您仅实现了onStartCommand(Intent, int, int, Location) ,该方法不属于服务生命周期的一部分,因此永远不会自动调用。 You should declare Location location as a local variable not as a parameter and annotate onStartCommand() with @Override to get warnings if the signatures don't match. 你应该申报Location location作为一个局部变量而不是作为一个参数和注释onStartCommand()@Override得到警告,如果签名不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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