我试图通过使用Fused位置提供程序api来获取位置,方法是在intentservice类中实现它,同时我在同一服务中启动newtimertask。
所以当调用intentservice时,timertask开始,googleapiclent连接起来获取位置。
我希望如果位置不可用,我的timertask会在接下来的60秒内断开googleapiclient。
但是这不起作用......如果它没有获得位置......意图服务继续运行或者融合服务提供者继续寻找位置。那么我必须停止这些吗?
package com.example.rj.intentlocationapi;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class BackgrndService extends IntentService implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
LocationManager mlocationmanger;
int bool = 0;
CountDownTimer cnt;
public BackgrndService() {
super("BackgrndService");
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
return false;
}
}
@Override
protected void onHandleIntent(Intent intent) {
if (!isGooglePlayServicesAvailable()) {
stopLocationService();
}
mlocationmanger = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
cnt = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.w("haha", "going to finish it");
stopLocationService();
}
}.start();
mGoogleApiClient.registerConnectionCallbacks(this);
mGoogleApiClient.connect();
Log.w("haha", "blocked to finish it");
}
public void onConnected(Bundle bundle) {
Log.w("hello", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.w("hhaahaa", "Connection suspended stop here ");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.w("hhaahaa", "Connection failed stop here ");
}
@Override
public void onLocationChanged(Location location) {
Log.w("hello", "Firing onLocationChanged..............................................");
mCurrentLocation = location;
if (mCurrentLocation != null) {
Log.w("hi", mCurrentLocation.getLatitude() + " " + mCurrentLocation.getLongitude());
}
stopLocationService();
}
public void stopLocationService() {
Log.w("haha", "killing begins");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
cnt.cancel();
Intent alarm = new Intent(this, BackgrndService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarm, 0);
if (bool == 0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 1000 * 100, pendingIntent);
bool = 1;
}
} }
答案 0 :(得分:0)
当onHandleIntent()结束时,如果在onHandleIntent()运行时没有向其发送更多命令,则IntentService会自动停止。因此,您不要自己手动停止IntentService。
答案 1 :(得分:0)
您可以将Intent-Service转移或更改为Service,并使用stopSelf()方法停止服务。此方法会停止服务。