2017-03-17 56 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

謝謝你這麼多,但有其他任何方式直接在此活動不需要打開它改爲設置活動 –