2015-05-02 101 views
0

我打電話給一個服務,我會在一定的時間間隔內獲得當前位置,並將該位置與傳遞給service.and的位置進行匹配,如果它處於近距離範圍內,它將嗡嗡聲電話。現在,我嘗試獲取當前位置通過FusedLocationApi。但我有錯誤,當我寫這部分在android中獲取服務中的重複當前位置?

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(),而不是這些,但同樣result.I正在呼叫服務我的整個代碼:

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) { 

    } 

實際上是什麼問題,我是去正確的路徑還是有任何簡單的方法來獲得當前位置在一定的時間間隔?

回答

2

您的代碼

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
          .addApi(LocationServices.API) 
          .addConnectionCallbacks(this) 
          .addOnConnectionFailedListener(this) 
          .build(); 

放在一個匿名的Runnable類的run()方法,這意味着this關鍵字指的是匿名運行的類,而不是你的服務類。因此,簡單地改變代碼,這將解決該問題:

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
          .addApi(LocationServices.API) 
          .addConnectionCallbacks(LocBuzzService.this) 
          .addOnConnectionFailedListener(LocBuzzService.this) 
          .build(); 

此外,該代碼並不需要非常多的時間來執行,所以你可以將它移出匿名Runnable接口的,它會作爲工作好。