【问题标题】:using Location Services fusedlocationapi in Android service -- Receiving only one location update在 Android 服务中使用 Location Services fusedlocationapi -- 只接收一个位置更新
【发布时间】:2017-07-01 16:31:28
【问题描述】:

我正在尝试制作一个后台服务,我想在应用关闭时运行它,并在应用打开时终止。

正如您在下面的代码中看到的,我有一条 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

【问题讨论】:

    标签: android service fusedlocationproviderapi


    【解决方案1】:
    if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    

    上述条件将删除位置更新,第一次后删除此代码

    removeLocationUpdates

    删除给定位置侦听器的所有位置更新。

    【讨论】:

    • 你太棒了!快捷方便。谢谢楼主!
    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 2016-02-17
    • 2011-10-03
    • 1970-01-01
    • 2021-12-15
    • 2016-05-30
    • 1970-01-01
    • 2013-05-27
    相关资源
    最近更新 更多