gpt4 book ai didi

android - 可以停止位置更新,安卓服务

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:28 24 4
gpt4 key购买 nike

我正在尝试创建一个路线跟踪应用程序。即使应用程序在后台,它也需要跟踪位置。所以我创建了一个服务并向该服务添加了代码。以下是我的代码。但有一个问题。我从我的主要 Activity 开始服务。

public void startTracking(View view) {
startService(new Intent(MainActivity.this, LocationIntentService.class));
}

public void stopTracking(View view) {
stopService(new Intent(MainActivity.this, LocationIntentService.class));
}

它启动服务并将位置插入到本地数据库。但我不能停止这些服务。当我使用上面的代码停止服务时,它仍然会跟踪位置。我怎样才能停止位置更新。

public class LocationIntentService extends IntentService implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = LocationIntentService.class.getSimpleName();
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
private static int DISPLACEMENT = 10;

LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
DBAdapter dbAdapter;

public LocationIntentService() {
super("LocationIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, " ***** Service on handled");
if (isGooglePlayServicesAvailable()) {
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}

@Override
public void onConnected(Bundle bundle) {
Log.e(TAG, " ***** Service on connected");
startLocationUpdates();
openDB();
}

@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, " ***** Service on suspended");
mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "Location changed");
mLastLocation = location;

String latitude = String.valueOf(mLastLocation.getLatitude());
String longitude = String.valueOf(mLastLocation.getLongitude());
Log.e(TAG, " ##### Got new location"+ latitude+ longitude);

Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String timestamp = today.format("%Y-%m-%d %H:%M:%S");

dbAdapter.insertRow(latitude, longitude, timestamp);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}

@Override
public void onDestroy() {
Log.e(TAG, "Service is Destroying...");
super.onDestroy();
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
closeDB();
}

protected void stopLocationUpdates() {
Log.d(TAG, "Location update stoping...");
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}

protected void startLocationUpdates() {
Log.d(TAG, "Location update starting...");
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);

}

private void openDB() {
dbAdapter = new DBAdapter(this);
dbAdapter.open();
}

private void closeDB() {
dbAdapter = new DBAdapter(this);
dbAdapter.close();
}

protected void createLocationRequest() {
Log.e(TAG, " ***** Creating location request");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
Log.e(TAG, " ***** Update google play service ");
return false;
}
}
}

最佳答案

它对您不起作用的原因是您正在使用 IntentService,因此调用 stopService() 不会导致 onDestroy() 被调用,大概是因为它已经在 onHandleIntent() 完成后被调用。无需在 IntentService 上调用 stopService() 参见 here .

看起来您应该只使用 Service 而不是 IntentService。这样,当您调用 stopService() 时,它会调用 onDestroy() 并取消注册位置更新,如您所料。

您唯一需要做的其他更改是覆盖 onStartCommand() 而不是 onHandleIntent()

您可以让您的类扩展 Service 而不是 IntentService,然后移动您的代码以注册位置更新到 onStartCommand:

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, " ***** Service on start command");
if (isGooglePlayServicesAvailable()) {
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
return Service.START_STICKY;
}

这样您仍然可以调用 startService()stopService(),它应该会像您期望的那样工作。

关于android - 可以停止位置更新,安卓服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com