Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. public void startGpsService() {
  2. handler = new Handler();
  3. task = new Runnable() {
  4. @Override
  5. public void run() {
  6. gps = new GPSTracker(getApplicationContext());
  7.  
  8. if (gps.canGetLocation()) {
  9. ... do something
  10. } else {
  11. // GPS or Network is not enabled
  12. gps.showSettingsAlert();
  13. }
  14.  
  15. handler.postDelayed(task, 10000);
  16. }
  17. };
  18. task.run();
  19. }
  20.  
  21. public class GPSTracker extends Service implements LocationListener {
  22.  
  23. private final Context mContext;
  24.  
  25. // Declaring a Location Manager
  26. protected LocationManager locationManager;
  27.  
  28. public GPSTracker(Context context) {
  29. this.mContext = context;
  30. getLocation();
  31. }
  32.  
  33. ...
  34.  
  35. /**
  36. * Function to show settings alert dialog
  37. * On pressing Settings button will lauch Settings Options
  38. * */
  39. public void showSettingsAlert(){
  40. AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  41.  
  42. // Setting Dialog Title
  43. alertDialog.setTitle("GPS is settings");
  44.  
  45. // Setting Dialog Message
  46. alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  47.  
  48. // On pressing Settings button
  49. alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  50. public void onClick(DialogInterface dialog,int which) {
  51. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  52. mContext.startActivity(intent);
  53. }
  54. });
  55.  
  56. // on pressing cancel button
  57. alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  58. public void onClick(DialogInterface dialog, int which) {
  59. dialog.cancel();
  60. }
  61. });
  62.  
  63. // Showing Alert Message
  64. alertDialog.show();
  65. }
  66. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement