2017-07-01 18 views
1

我正在嘗試製作一個後臺服務,當應用程序關閉時運行,並在應用程序打開時終止。在Android服務中使用定位服務fusedlocationapi - 只接收一個位置更新

正如你可以在下面的代碼中看到的,我有一個Toast消息,應該在onLocationChanged()中顯示。我只看到該消息出現一次。

這裏是允許的樣子在清單:

<application 
... 

<activity android:name=".activities.ChatUsersActivity" /> 
    <activity android:name=".activities.RequestsActivity"></activity> 
    <service android:name=".pops.PopService" 
     android:exported="false" 
     android:icon="@drawable/usericonmdpi"/> 

</application> 

而且也,這裏是我的服務擴展類:

public class PopService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(2); 
Runnable backgroundCollector = new BackgroundPopCollector(); 

private GoogleApiClient mGoogleApiClient; 
LocationRequest mLocationRequest; 
Location mLastLocation; 

private PopCityCollection cities; 

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



@Override 
public void onCreate() { 
    // Code to execute when the service is first created 
    super.onCreate(); 
    buildGoogleApiClient(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    threadPoolExecutor.shutdownNow(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 
    UserLocation.setLatitude(location.getLatitude()); 
    UserLocation.setLongitude(location.getLongitude()); 
    Toast.makeText(getBaseContext(), "Latitude is " + location.getLatitude(), Toast.LENGTH_SHORT).show(); 
    System.out.println(UserLocation.getLatitude()+", "+UserLocation.getLongitude()+" --->BACKGROUND!!"); 
    if(cities==null) { 
     cities = new PopCityCollection(new LatLng(location.getLatitude(), location.getLongitude())); 
     cities.findMyCity(this); 
    } 
    DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("pops"); 
    db.push().setValue("test"); 



} 


@Override 
public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 

    mLocationRequest.setInterval(100); 
    mLocationRequest.setFastestInterval(100); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    if (ContextCompat.checkSelfPermission(this, 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId){ 
    threadPoolExecutor.scheduleAtFixedRate(backgroundCollector, 0, 10, TimeUnit.SECONDS); 
    return START_NOT_STICKY; 
} 

} 

感謝您能提供任何見解!

-T

回答

2
if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 

上述條件將刪除位置更新,第一次經過這麼刪除此代碼

removeLocationUpdates

刪除對於給定的位置監聽器的所有位置更新。

+1

你太棒了!快捷方便。謝謝你,先生! – TJBlack31