2017-03-17 9 views

答えて

0

GPSロケーションをオンにするには、この機能を使用してください。

private void buildAlertMessageNoGps() { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
         dialog.cancel(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         dialog.cancel(); 
        } 
       }); 
     final AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

使用するメッセージは何でも使用できます。

編集:

直接これを使用する上でGPSをオンにする: -

LocationRequest locationRequest = LocationRequest.create(); 
       locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
       locationRequest.setInterval(30 * 1000); 
       locationRequest.setFastestInterval(5 * 1000); 
       LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
         .addLocationRequest(locationRequest); 

       builder.setAlwaysShow(true); 

       PendingResult<LocationSettingsResult> result = 
         LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
       result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
        @Override 
        public void onResult(LocationSettingsResult result) { 
         final Status status = result.getStatus(); 
         switch (status.getStatusCode()) { 
          case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
           try { 
            // Show the dialog by calling startResolutionForResult(), 
            // and check the result in onActivityResult(). 
            status.startResolutionForResult(
              MainActivity.this, 101); 
           } catch (IntentSender.SendIntentException e) { 
            // Ignore the error. 
           } 
           break; 
         } 
        } 
       }); 
+0

はどうもありがとう、これに必要のないこの活動に直接の電源をオンにする他にどのような方法があります設定活動に変更 –

関連する問題