Advertisement
Guest User

Untitled

a guest
Dec 20th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1.  
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.location.GpsStatus.Listener;
  8. import android.location.LocationManager;
  9.  
  10. public class GpsCheck{
  11. public Context context;
  12. LocationManager manager;
  13. public GpsCheck(Context context){
  14. this.context=context;
  15. this.manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  16. Listener gpsStatusListener = new Listener(){
  17.  
  18. public void onGpsStatusChanged(int arg0) {
  19. gpsEnable();
  20.  
  21. }
  22. };
  23. this.manager.addGpsStatusListener(gpsStatusListener);
  24. }
  25.  
  26. public boolean gpsEnable(){
  27. //checks weather the GPS is enabled. If not, prompts the user to enable it then checks again. Returns false if the user chooses not to enable the application.
  28. if(!checkGpsEnabled()){
  29. promptEnableGps();
  30. }
  31. return true;
  32. }
  33.  
  34. private boolean checkGpsEnabled(){
  35. this.manager = (LocationManager)context. getSystemService(Context.LOCATION_SERVICE);
  36. if ( !this.manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
  37. return false;
  38. }
  39. return true;
  40. }
  41.  
  42. private boolean promptNoGpsQuit(){
  43. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
  44. alertDialogBuilder.setMessage("Without enabling GPS this app can not function, Would you like to quit?");
  45. alertDialogBuilder.setCancelable(false);
  46. DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
  47. public void onClick(DialogInterface dialog, int id){
  48. Intent callGPSSettingIntent = new Intent(
  49. android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  50. context.startActivity(callGPSSettingIntent);
  51. }
  52. };
  53. alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
  54.  
  55. DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
  56. public void onClick(DialogInterface dialog, int id){
  57. dialog.cancel();
  58.  
  59. }
  60. };
  61. alertDialogBuilder.setNegativeButton("Quit", onClickCancel);
  62. AlertDialog alert = alertDialogBuilder.create();
  63. alert.show();
  64. return true;
  65. }
  66.  
  67. private boolean promptEnableGps(){
  68. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
  69. alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?");
  70. alertDialogBuilder.setCancelable(false);
  71. DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
  72. public void onClick(DialogInterface dialog, int id){
  73. Intent callGPSSettingIntent = new Intent(
  74. android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  75. context.startActivity(callGPSSettingIntent);
  76. }
  77. };
  78. alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
  79.  
  80. DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
  81. public void onClick(DialogInterface dialog, int id){
  82. dialog.cancel();
  83. promptNoGpsQuit();
  84. }
  85. };
  86. alertDialogBuilder.setNegativeButton("Cancel", onClickCancel);
  87. AlertDialog alert = alertDialogBuilder.create();
  88. alert.show();
  89. return true;
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement