2016-06-23 10 views
0

GoogleマップがGPSを有効にするようにユーザーに促すことができたことに気づきました。私はこのメソッドを以下で試してみましたが、一度だけプロンプトが表示されたら何もしません...なぜ、onActivityResult()メソッドがもう呼び出されないのですか?GPSを有効にするためのポップアップをユーザーに表示

public void checkLocationEnable() { 
    Log.e(TAG, "Here"); 
    LocationRequest mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(10000); 
    mLocationRequest.setFastestInterval(5000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 

    final PendingResult<LocationSettingsResult> result = 
      LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 
        builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { 
      final Status status = locationSettingsResult.getStatus(); 
      final LocationSettingsStates state = locationSettingsResult.getLocationSettingsStates(); 
      Log.e(TAG, "state:" + state); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        Log.e(TAG, "HERE-1"); 
        // All location settings are satisfied. The client can 
        // initialize location requests here. 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        Log.e(TAG, "REQUIRED"); 
        // Location settings are not satisfied, but this can be fixed 
        // by showing the user a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(
           getActivity(), 
           GenericActivity.REQUEST_LOCATION); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way 
        // to fix the settings so we won't show the dialog. 
        Log.e(TAG, "UNAVALAIBLE"); 

        break; 
      } 
     } 
    }); 
} 
+0

ユーザーがダイアログボックスに応答すると、このメソッドは結果を受け取るように、onActivityResultメソッドをオーバーライドする必要があります。 –

+0

私はそれをActivityHome – Jaythaking

+0

[this](http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is)で上書きします-enabled/843716#comment63478757_843716)あなたが求めていることをします。 – TDG

答えて

2

私の知る限り、場所を有効にしてください。

ステップバイステップのプロセス。

まず、GoogleApiClientを設定し、GoogleApiClient CallBackMethodsを実装する必要があります。

protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API).build(); 
     mGoogleApiClient.connect(); 
} 

コールバックメソッド

@Override 
public void onConnected(Bundle bundle) { 
    Log.d("OnConnected", "Connection successful"); 
    settingsrequest(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode()); 
} 

とすぐgoogleapiclientが接続されているとして、onconnected方法が起動しますとsettingsRequest方法を呼び出します。

protected static final int REQUEST_CHECK_SETTINGS = 0x1; 
LocationRequest locationRequest; 


public void settingsrequest() { 
    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    locationRequest.setInterval(30 * 1000); 
    locationRequest.setNumUpdates(1); 
    locationRequest.setExpirationDuration(20000); 
    locationRequest.setFastestInterval(500); 
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); 
    builder.setAlwaysShow(true); //this is the key ingredient 
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult result) { 
      final Status status = result.getStatus(); 
      final LocationSettingsStates state = result.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: { 
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 
       } 
       // All location settings are satisfied. The client can initialize location 
       // requests here. 
       break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        // Location settings are not satisfied. But could be fixed by showing the user 
        // a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(MainTab.this, REQUEST_CHECK_SETTINGS); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way to fix the 
        // settings so we won't show the dialog. 
        break; 
      } 
     } 
    }); 
} 

status.startResolutionForResult(MainTab.this、REQUEST_CHECK_SETTINGS)が場所を要求するダイアログボックスが表示されます。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    // Check for the integer request code originally supplied to startResolutionForResult(). 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 
        break; 
       case Activity.RESULT_CANCELED: 
        break; 
      } 
      break; 
    } 
} 

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient、なLocationRequest、これは)位置cahngedのリスナーを設定します。

場所が変更されたときに、onLocationChangedメソッドが呼び出されます。

@Override 
public void onLocationChanged(Location location) { 
     prevLocation = location; 
     //Do you work 
} 

これが役立ちます。

+0

私はこれらのステップがGoogle developperページでうまく説明されているので、これをすべて試しました...まだ、ポップアップは表示されません – Jaythaking

+0

テスト中にどのAndroid OSバージョンを使用していますか? –

+0

APIを使用しているIm 22 – Jaythaking

関連する問題