2016-05-15 143 views
0

我有調用服務的活動(主要活動)。該服務需要在後臺運行並查找附近的位置。Android服務未啓動

問題是服務沒有做任何事情(它不會輸入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 
} 
} 
+0

您是否在該服務中聲明瞭對您的許可? –

+0

@PavelDurov當然可以。 –

回答

1

你必須重載Service.onStartCommand(Intent, int, int),但不知何故,你只實現了一個方法onStartCommand(Intent, int, int, Location)這不是服務的生活的一部分cyle,因此永遠不會自動調用。如果簽名不匹配,則應聲明Location location作爲局部變量而不是參數並註釋onStartCommand()@Override以獲得警告。

+0

現在感謝它的工作 –