2016-05-15 10 views
0

Ich habe Aktivität (Hauptaktivität), die zu einem Dienst anruft. Der Dienst muss im Hintergrund ausgeführt werden und nach Standorten in der Nähe suchen.Android-Dienst nicht starten

Das Problem ist, dass der Dienst nichts tut (es gibt onStartCommand nicht ein).

Ich würde mich freuen, wenn Sie mir helfen können, das Problem zu verstehen und zu beheben.

Haupt Aufruf zum 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 
} 
} 
+0

Haben Sie in Ihrem Manifest für diesen Service eine Berechtigung deklariert? –

+0

@PavelDurov Ja natürlich. –

Antwort

1

Sie haben Service.onStartCommand(Intent, int, int), außer Kraft zu setzen, aber irgendwie haben Sie implementiert nur eine Methode onStartCommand(Intent, int, int, Location), die nicht Teil des Lebens des Dienstes ist cyle und somit nie automatisch aufgerufen. Sie sollten Location location als lokale Variable nicht als Parameter deklarieren und onStartCommand() mit @Override annotieren, um Warnungen zu erhalten, wenn die Signaturen nicht übereinstimmen.

+0

Danke jetzt funktioniert es –