2016-07-22 31 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

相关问题