Advertisement
ScottHelme

Android Check Location Services With Prompt

Dec 21st, 2012
1,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. // string to store location providers available;
  2. private String _mProviders;
  3.  
  4. // get location providers
  5. _mProviders = Settings.Secure.getString(getContentResolver(),
  6.         Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  7.  
  8. // if gps is enabled and network location is disabled
  9. if (_mProviders.contains(_GPS) && !_mProviders.contains(_NETWORK)) {
  10.  
  11.     // build a new alert dialog to inform the user that they only have
  12.     // GPS location services
  13.     new AlertDialog.Builder(_mContext)
  14.    
  15.             //set the message to display to the user
  16.             .setMessage(R.string.onlyGPS)
  17.  
  18.             // add the 'positive button' to the dialog and give it a
  19.             // click listener
  20.             .setPositiveButton(R.string.enableLocationServices,
  21.                     new DialogInterface.OnClickListener() {
  22.                         // setup what to do when clicked
  23.                         public void onClick(DialogInterface dialog,
  24.                                 int id) {
  25.                             // start the settings menu on the correct
  26.                             // screen for the user
  27.                             startActivity(new Intent(
  28.                                     Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  29.                         }
  30.  
  31.                         // add the 'negative button' to the dialog and
  32.                         // give it a click listener
  33.                     })
  34.             .setNegativeButton(R.string.close,
  35.                     new DialogInterface.OnClickListener() {
  36.                         // setup what to do when clicked
  37.                         public void onClick(DialogInterface dialog,
  38.                                 int id) {
  39.                             // remove the dialog
  40.                             dialog.cancel();
  41.                         }
  42.  
  43.                         // finish creating the dialog and show to the
  44.                         // user
  45.                     }).create().show();
  46.  
  47.     // if gps is disabled and network location services are enabled
  48. } else if (!_mProviders.contains(_GPS)
  49.         && _mProviders.contains(_NETWORK)) {
  50.  
  51.     // do nothing at present...
  52.  
  53.     // if gps is disabled and network location services are disabled
  54. } else if (!_mProviders.contains(_GPS)
  55.         && !_mProviders.contains(_NETWORK)) {
  56.  
  57.     // build a new alert dialog to inform the user that they have no
  58.     // location services enabled
  59.     new AlertDialog.Builder(_mContext)
  60.    
  61.             //set the message to display to the user
  62.             .setMessage(R.string.noLocationServices)
  63.  
  64.             // add the 'positive button' to the dialog and give it a
  65.             // click listener
  66.             .setPositiveButton(R.string.enableLocationServices,
  67.                     new DialogInterface.OnClickListener() {
  68.                         // setup what to do when clicked
  69.                         public void onClick(DialogInterface dialog,
  70.                                 int id) {
  71.                             // start the settings menu on the correct
  72.                             // screen for the user
  73.                             startActivity(new Intent(
  74.                                     Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  75.                         }
  76.  
  77.                         // add the 'negative button' to the dialog and
  78.                         // give it a click listener
  79.                     })
  80.             .setNegativeButton(R.string.close,
  81.                     new DialogInterface.OnClickListener() {
  82.                         // setup what to do when clicked
  83.                         public void onClick(DialogInterface dialog,
  84.                                 int id) {
  85.                             // remove the dialog
  86.                             dialog.cancel();
  87.                         }
  88.  
  89.                         // finish creating the dialog and show to the
  90.                         // user
  91.                     }).create().show();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement