在android的服务中获取重复的当前位置?
Posted
技术标签:
【中文标题】在android的服务中获取重复的当前位置?【英文标题】:Getting Repeated Current Location inside a Service in android? 【发布时间】:2015-05-02 20:03:43 【问题描述】:我正在调用服务,如果我将在某个时间间隔获取当前位置,并将该位置与传递给服务的位置相匹配。如果它在近距离范围内,它会响起电话。现在我试图获取当前位置FusedLocationApi
的位置。但是我写这部分时出错了
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)//here i got the error
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
我在编译器中遇到错误,例如:
`addConnectionCallbacks` in builder can't be applied to java.lang.Runnable
我试图给getApplicationContext()
而不是这些但结果相同。我正在从片段中调用服务类。我的整个代码:
public class LocBuzzService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static final String TAG = "HelloService";
private boolean isRunning = false;
public Vibrator vibrator;
@Override
public void onCreate()
Log.i(TAG, "Service onCreate");
isRunning = true;
@Override
public int onStartCommand(final Intent intent, int flags, int startId)
Log.i(TAG, "Service onStartCommand");
new Thread(new Runnable()
@Override
public void run()
try
//Bundle b=intent.getExtras();
//Double lat = b.getDouble("lat");
//Double lng = b.getDouble("lng");
//vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
//vibrator.vibrate(3000);
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
catch (Exception e)
Log.e(TAG, Log.getStackTraceString(e));
// stopSelf();
).start();
return Service.START_STICKY;
@Override
public IBinder onBind(Intent intent)
Log.i(TAG, "Service onBind");
return null;
@Override
public void onDestroy()
Log.i(TAG, "Service onDestroy");
isRunning = false;
mGoogleApiClient.disconnect();
@Override
public void onConnected(Bundle bundle)
Log.i(TAG, "Onconnected");
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(2000); // Update location every second
//LocationServices.FusedLocationApi.requestLocationUpdates(
//mGoogleApiClient, mLocationRequest,this);
@Override
public void onConnectionSuspended(int i)
Log.i(TAG, "Connection Suspended");
@Override
public void onLocationChanged(Location location)
Log.i(TAG, "LocationChanged");
Double lat1 = location.getLatitude();
Double lng1 = location.getLongitude();
Log.i(TAG,String.valueOf(lat1)+ ", " + String.valueOf(lng1));
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
@Override
public void onProviderDisabled(String provider)
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
实际上有什么问题,我走的是正确的道路还是有什么简单的方法可以在某个时间间隔获取当前位置?
【问题讨论】:
【参考方案1】:你的代码
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
放在匿名 Runnable 类的 run() 方法中,这意味着 this
关键字指的是该匿名 Runnable 类而不是您的 Service 类。因此,只需将代码更改为此即可解决问题:
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(LocBuzzService.this)
.addOnConnectionFailedListener(LocBuzzService.this)
.build();
另外,这段代码执行的时间并不长,因此您只需将其移出匿名 Runnable 即可。
【讨论】:
以上是关于在android的服务中获取重复的当前位置?的主要内容,如果未能解决你的问题,请参考以下文章