How to ask user to enable GPS at the launch of application?

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);  if(!provider.contains("gps")){ //if gps is disabled     final Intent poke = new Intent();     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");      poke.addCategory(Intent.CATEGORY_ALTERNATIVE);     poke.setData(Uri.parse("3"));      sendBroadcast(poke); } } 

see the Logcat

I tried many other option like package manager, location manager, etc but not able to do what I want. I want that at the launch application user will be prompted to enable the GPS. I don't want user to go inside the settings.

And that I wanted for the API level >=19.

Please help me to solve my problem......... Thanks

回答1:

 GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)             .addApi(LocationServices.API)             .addConnectionCallbacks(this)             .addOnConnectionFailedListener(this).build();     googleApiClient.connect();      LocationRequest locationRequest = LocationRequest.create();     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);     locationRequest.setInterval(5 * 1000);     locationRequest.setFastestInterval(2 * 1000);     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()             .addLocationRequest(locationRequest);      //**************************     builder.setAlwaysShow(true); //this is the key ingredient     //**************************      PendingResult<LocationSettingsResult> result =             LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());     result.setResultCallback(new ResultCallback<LocationSettingsResult>() {         @Override         public void onResult(@NonNull LocationSettingsResult result) {             final Status status = result.getStatus(); //                final LocationSettingsStates state = result.getLocationSettingsStates();              switch (status.getStatusCode()) {                 case LocationSettingsStatusCodes.SUCCESS:                       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(                                 context, 1000);                     } catch (IntentSender.SendIntentException e) {                         // Ignore the error.                     }                     break;                 case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:                     break;             }         }     }); 


回答2:

use SettingsApi if you not want to force user to go settings page to enable gps. here the link of how to use SettingsApi SettingsApi



回答3:

public void showSettingAlert()     {         AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);         alertDialog.setTitle("GPS setting!");         alertDialog.setMessage("GPS is not enabled, Do you want to go to settings menu? ");         alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {             @Override             public void onClick(DialogInterface dialog, int which) {                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                 context.startActivity(intent);             }         });         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {             @Override             public void onClick(DialogInterface dialog, int which) {                 dialog.cancel();             }         });         alertDialog.show();     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!