Advertisement
rachmadi

Current Location

Jan 11th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. public class MainActivity extends Activity implements LocationListener {
  2.  
  3.      private LocationManager locationManager;
  4.        
  5.        
  6.         @Override
  7.         protected void onCreate(Bundle savedInstanceState) {
  8.             super.onCreate(savedInstanceState);
  9.             setContentView(R.layout.activity_main);
  10.             locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  11.            
  12.            
  13.             Button bt=(Button) findViewById(R.id.btn1);
  14.            
  15.             bt.setOnClickListener(new View.OnClickListener() {
  16.                
  17.                 @Override
  18.                 public void onClick(View v) {
  19.                     // TODO Auto-generated method stub
  20.                
  21.                      // getting GPS status
  22.                     boolean isGPSEnabled = locationManager
  23.                             .isProviderEnabled(LocationManager.GPS_PROVIDER);
  24.          
  25.  
  26.                    
  27.                    
  28.                      if(!isGPSEnabled)
  29.                     {
  30.                           AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  31.                          
  32.                           // Setting Dialog Title
  33.                           alertDialog.setTitle("GPS settings");
  34.                    
  35.                           // Setting Dialog Message
  36.                           alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  37.                    
  38.                           // On pressing Settings button
  39.                           alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  40.                               public void onClick(DialogInterface dialog,int which) {
  41.                                   Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  42.                                   startActivity(intent);
  43.                               }
  44.                           });
  45.                    
  46.                           // on pressing cancel button
  47.                           alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  48.                               public void onClick(DialogInterface dialog, int which) {
  49.                               dialog.cancel();
  50.                               }
  51.                           });
  52.                    
  53.                           // Showing Alert Message
  54.                           alertDialog.show();
  55.                        
  56.                     }
  57.                      else
  58.                      {
  59.                          if (locationManager != null) {
  60.                            Location location = locationManager
  61.                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  62.                            if (location != null) {
  63.                                double latitude = location.getLatitude();
  64.                                double longitude = location.getLongitude();
  65.                            Toast.makeText(getApplication(),"latitude: "+ latitude +" longitude: "+longitude, Toast.LENGTH_LONG).show();
  66.                            
  67.                            }
  68.                      }
  69.                 }
  70.                      }
  71.             });
  72.              
  73.         }
  74.  
  75.  
  76.         @Override
  77.         public void onLocationChanged(Location location) {
  78.             // TODO Auto-generated method stub
  79.            
  80.         }
  81.  
  82.  
  83.         @Override
  84.         public void onProviderDisabled(String provider) {
  85.             // TODO Auto-generated method stub
  86.            
  87.         }
  88.  
  89.  
  90.         @Override
  91.         public void onProviderEnabled(String provider) {
  92.             // TODO Auto-generated method stub
  93.            
  94.         }
  95.  
  96.  
  97.         @Override
  98.         public void onStatusChanged(String provider, int status, Bundle extras) {
  99.             // TODO Auto-generated method stub
  100.            
  101.         }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement