2016-07-22 80 views
0

我試圖讓我的應用程序通過GooglePlayServices訪問位置。GoogleServicesAPI - 應用程序不檢查GPS是否啓用

我有我的應用程序連接到互聯網,但當我意識到手機不檢查GPS是否打開時,我已經擱淺了一個問題。

如果GPS開啓,手機會自動開始更新。但是,當GPS關閉時,手機根本沒有反應。

我已經研究過並且遇到過onProviderEnabled,但我只看到它適用於LocationManager,並且不確定它是否也適用於GooglePlayAPI。

如何讓我的應用程序檢查GPS是否打開?

onConnected():

public void onConnected(Bundle connectionHint) { 
    Toast.makeText(this, "You have connected", Toast.LENGTH_LONG).show(); 

    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(UPDATE_INTERVAL); 
    mLocationRequest.setFastestInterval(UPDATE_INTERVAL); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this); 

     Location NewLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient); 

     //Getting initial location and time 
     PhoneBelt.setNewLocation(NewLocation); 
     long NewLocationTime = new Date().getTime(); 
     PhoneBelt.setNewLocationTime(NewLocationTime); 
    } 
} 

回答

1

使用下面的代碼,它使用googleSettingsAPI檢查位置啓用。如果不是,它會顯示類似一個對話框,以谷歌地圖:

protected void buildLocationSettingsRequest() { 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); 
    builder.addLocationRequest(mLocationRequest); 
    builder.setAlwaysShow(true); 
    mLocationSettingsRequest = builder.build(); 
    } 


    protected void checkLocationSettings() { 
    PendingResult<LocationSettingsResult> result = 
      LocationServices.SettingsApi.checkLocationSettings(
        mGoogleApiClient, 
        mLocationSettingsRequest 
      ); 
    result.setResultCallback(this); 
    } 


@Override 
public void onResult(LocationSettingsResult locationSettingsResult) { 
    final Status status = locationSettingsResult.getStatus(); 

    switch (status.getStatusCode()) { 


     case LocationSettingsStatusCodes.SUCCESS: 
      Log.d("FragmentCreate", "All location settings are satisfied."); 

      pd.show(); 


      try { 

       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 


      }catch (SecurityException se){ 
       Log.d("FragmentCreate","You don't have permissions"); 
       pd.dismiss(); 
       errortext.setVisibility(View.VISIBLE); 
       errortext.setText("Please provide Location permission to continue, Settings->Apps->RecommendedApp->Permissions"); 
       Toast.makeText(this,"Please provide location permissions to continue",Toast.LENGTH_SHORT).show(); 
      } 


      break; 


     case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
      Log.d("FragmentCreate", "Location settings are not satisfied. Show the user a dialog to" + 
        " upgrade location settings "); 

      try { 
       // Show the dialog by calling startResolutionForResult(), and check the result 
       // in onActivityResult(). 
       status.startResolutionForResult(TabbedResult.this, REQUEST_CHECK_SETTINGS); 
      } catch (IntentSender.SendIntentException e) { 
       Log.i("FragmentCreate", "PendingIntent unable to execute request."); 
      } 
      break; 



     case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
      Log.d("FragmentCreate", "Location settings are inadequate, and cannot be fixed here. Dialog " + 
        "not created."); 
      break; 
    } 
} 

mGoogleApiClient是你建立你的googleApiClient對象,並LocationSettingsRequest是請求您使用來獲取供應商

+0

所以我只是把這個在onConnected? –

+0

在onCreate中構建位置設置並調用onConnected – Kushan

+0

中的checkLocationSetting確保在調用onConnected – Kushan

相關問題