2015-06-30 22 views
1

我试图获取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)

想法?非常感谢!

+1

将MeasurmentService.class更改为MeasurmentService.this – PLP

+0

* java.lang.NoClassDefFoundError:packet.MeasurementService *将意味着运行时没有找到.class来运行它..尝试重建项目 –

回答

1

确保位置监听器实施的服务来自com.google.android.gms.location

移动代码到onCreate函数。

@Override 
    public void onCreate() { 
     mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
         .addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this) 
         .addApi(LocationServices.API) 
         .build(); 

       mGoogleApiClient.connect(); 
} 

我在github上有一个示例应用程序,请点击here

+0

我有这样的:import com。 google.android.gms.location.LocationListener;所以应该是对的。 – Manu

+0

尝试将代码放入onCreate。我有一个应用程序具有相同的要求,并且它工作正常,如果这不起作用,我只会告诉你我的源代码。 –

+0

@kevindi迁移你2个答案之一, –

0

我终于明白了,这是一个构建错误,我无法解决。

我的解决方案:

我将项目从Eclipse迁移到Android Studio。