Advertisement
ScottHelme

Location Services Check

Dec 8th, 2012
3,679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. // if gps is enabled and network location is disabled
  2. // note, GPS can be slow to obtain location data but is more accurate
  3. if (locationProviders.contains("gps") && !locationProviders.contains("network")) {
  4.  
  5.     // build a new alert dialog to inform the user that they only have GPS location services
  6.     new AlertDialog.Builder(this)
  7.    
  8.             //set the message to display to the user
  9.             .setMessage("Only GPS Enabled")
  10.  
  11.             // add the 'positive button' to the dialog and give it a
  12.             // click listener
  13.             .setPositiveButton("Enable Location Services",
  14.                     new DialogInterface.OnClickListener() {
  15.                         // setup what to do when clicked
  16.                         public void onClick(DialogInterface dialog,
  17.                                 int id) {
  18.                             // start the settings menu on the correct
  19.                             // screen for the user
  20.                             startActivity(new Intent(
  21.                                     Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  22.                         }
  23.  
  24.                         // add the 'negative button' to the dialog and
  25.                         // give it a click listener
  26.                     })
  27.             .setNegativeButton("Close",
  28.                     new DialogInterface.OnClickListener() {
  29.                         // setup what to do when clicked
  30.                         public void onClick(DialogInterface dialog,
  31.                                 int id) {
  32.                             // remove the dialog
  33.                             dialog.cancel();
  34.                         }
  35.  
  36.                         // finish creating the dialog and show to the
  37.                         // user
  38.                     }).create().show();
  39.  
  40.     // if gps is disabled and network location services are enabled
  41.         // network services are quick and fairly accurate, we are happy to use them
  42. } else if (!locationProviders.contains("gps") && locationProviders.contains("network")) {
  43.  
  44.     // do nothing at present...
  45.  
  46.     // if gps is disabled and network location services are disabled
  47.         // the user has no location services and must enable something
  48. } else if (!locationProviders.contains("gps") && !locationProviders.contains("network")) {
  49.  
  50.     // build a new alert dialog to inform the user that they have no
  51.     // location services enabled
  52.     new AlertDialog.Builder(this)
  53.    
  54.             //set the message to display to the user
  55.             .setMessage("No Location Services Enabled")
  56.  
  57.             // add the 'positive button' to the dialog and give it a
  58.             // click listener
  59.             .setPositiveButton("Enable Location Services",
  60.                     new DialogInterface.OnClickListener() {
  61.                         // setup what to do when clicked
  62.                         public void onClick(DialogInterface dialog,
  63.                                 int id) {
  64.                             // start the settings menu on the correct
  65.                             // screen for the user
  66.                             startActivity(new Intent(
  67.                                     Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  68.                         }
  69.  
  70.                         // add the 'negative button' to the dialog and
  71.                         // give it a click listener
  72.                     })
  73.             .setNegativeButton("Close",
  74.                     new DialogInterface.OnClickListener() {
  75.                         // setup what to do when clicked
  76.                         public void onClick(DialogInterface dialog,
  77.                                 int id) {
  78.                             // remove the dialog
  79.                             dialog.cancel();
  80.                         }
  81.  
  82.                         // finish creating the dialog and show to the
  83.                         // user
  84.                     }).create().show();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement