我试图获取android服务中手机的当前GPS位置。 我尝试做它喜欢这里:获取android中的当前服务位置
Getting Repeated Current Location inside a Service in android?
我的服务是这样的:
public class MeasurementService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private Feedback feedback;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
feedback = new Feedback();
if (isNetworkAvailable()) {
new MeasuringTask().execute();
}
return Service.START_NOT_STICKY;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
@Override
public void onConnected(Bundle arg0) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(2000);
...
}
@Override
public void onConnectionSuspended(int arg0) {
}
@Override
public void onDestroy() {
super.onDestroy();
mGoogleApiClient.disconnect();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
private class MeasuringTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
//network access
}
}
我通过广播接收器每次手机启动时启动我的服务。 当我执行的应用程序,我得到一个java.lang.NoClassDefFoundError:packet.MeasurementService
如果我试图改变这一部分:
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
要这样:
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(MeasurmentService.class)
.addOnConnectionFailedListener(MeasurmentService.class)
.addApi(LocationServices.API)
.build();
像推荐另一个帖子,我得到:
类型GoogleApiClient.Builder中的方法addConnectionCallbacks(GoogleApiClient.ConnectionCallbacks)不适用(Class)
想法?非常感谢!
将MeasurmentService.class更改为MeasurmentService.this – PLP
* java.lang.NoClassDefFoundError:packet.MeasurementService *将意味着运行时没有找到.class来运行它..尝试重建项目 –