Advertisement
kernel_memory_dump

Android GPS

May 13th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. package com.example.ilija.myapplication;
  2.  
  3. import android.content.Context;
  4. import android.location.Address;
  5. import android.location.Geocoder;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.support.v7.app.ActionBarActivity;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.widget.Toast;
  15.  
  16. import java.io.IOException;
  17. import java.util.List;
  18. import java.util.Locale;
  19.  
  20. import static android.location.LocationManager.GPS_PROVIDER;
  21.  
  22.  
  23. public class MainActivity extends ActionBarActivity {
  24.  
  25.     private double latitude;
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         LocationManager locationManager = (LocationManager)
  38.                 getSystemService(Context.LOCATION_SERVICE);
  39.  
  40.         MyLocationListener locationListener = new MyLocationListener();
  41.         locationManager.requestLocationUpdates(
  42.                 GPS_PROVIDER, 5000, 10, locationListener);
  43.  
  44.         locationListener.getLocation(locationManager);
  45.  
  46.  
  47.     }
  48.  
  49.  
  50.     @Override
  51.     public boolean onCreateOptionsMenu(Menu menu) {
  52.         // Inflate the menu; this adds items to the action bar if it is present.
  53.         getMenuInflater().inflate(R.menu.menu_main, menu);
  54.         return true;
  55.     }
  56.  
  57.     @Override
  58.     public boolean onOptionsItemSelected(MenuItem item) {
  59.         // Handle action bar item clicks here. The action bar will
  60.         // automatically handle clicks on the Home/Up button, so long
  61.         // as you specify a parent activity in AndroidManifest.xml.
  62.         int id = item.getItemId();
  63.  
  64.         //noinspection SimplifiableIfStatement
  65.         if (id == R.id.action_settings) {
  66.             return true;
  67.         }
  68.  
  69.         return super.onOptionsItemSelected(item);
  70.     }
  71.  
  72.  
  73.     /*---------- Listener class to get coordinates ------------- */
  74.     private class MyLocationListener implements LocationListener {
  75.  
  76.         @Override
  77.         public void onLocationChanged(Location loc) {
  78.             Toast.makeText(
  79.                     getBaseContext(),
  80.                     "Location changed: Lat: " + loc.getLatitude() + " Lng: "
  81.                             + loc.getLongitude(), Toast.LENGTH_SHORT).show();
  82.             String longitude = "Longitude: " + loc.getLongitude();
  83.             Log.v("a", longitude);
  84.             String latitude = "Latitude: " + loc.getLatitude();
  85.             Log.v("a", latitude);
  86.  
  87.         /*------- To get city name from coordinates -------- */
  88.             String cityName = null;
  89.             Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
  90.             List<Address> addresses;
  91.             try {
  92.                 addresses = gcd.getFromLocation(loc.getLatitude(),
  93.                         loc.getLongitude(), 1);
  94.                 if (addresses.size() > 0)
  95.                     System.out.println(addresses.get(0).getLocality());
  96.                 cityName = addresses.get(0).getLocality();
  97.             }
  98.             catch (IOException e) {
  99.                 e.printStackTrace();
  100.             }
  101.             String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
  102.                     + cityName;
  103.         }
  104.  
  105.         @Override
  106.         public void onProviderDisabled(String provider) {}
  107.  
  108.         @Override
  109.         public void onProviderEnabled(String provider) {}
  110.  
  111.         @Override
  112.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  113.  
  114.  
  115.  
  116.  
  117.     /**
  118.      * Function to get the user's current location
  119.      *
  120.      * @return
  121.      */
  122.     public Location getLocation(LocationManager locationManager){
  123.         double latitude = 0;
  124.         double longitude = 0;
  125.         Location location = null;
  126.         try {
  127.  
  128.  
  129.  
  130.             // getting GPS status
  131.             boolean isGPSEnabled = locationManager
  132.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
  133.  
  134.             Log.v("isGPSEnabled", "=" + isGPSEnabled);
  135.  
  136.             // getting network status
  137.             boolean  isNetworkEnabled = locationManager
  138.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  139.  
  140.             Log.v("isNetworkEnabled", "=" + isNetworkEnabled);
  141.  
  142.             if (isGPSEnabled == false && isNetworkEnabled == false) {
  143.                 // no network provider is enabled
  144.             } else {
  145.                 if (isNetworkEnabled) {
  146.                     location=null;
  147.                     Log.d("Network", "Network");
  148.                     if (locationManager != null) {
  149.                         location = locationManager
  150.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  151.                         if (location != null) {
  152.                             latitude = location.getLatitude();
  153.                             longitude = location.getLongitude();
  154.                         }
  155.                     }
  156.                 }
  157.                 // if GPS Enabled get lat/long using GPS Services
  158.                 if (isGPSEnabled) {
  159.                     location=null;
  160.                         Log.d("GPS Enabled", "GPS Enabled");
  161.                         if (locationManager != null) {
  162.                             location = locationManager
  163.                                     .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  164.                             if (location != null) {
  165.                                 latitude = location.getLatitude();
  166.                                 longitude = location.getLongitude();
  167.                             }
  168.                         }
  169.  
  170.                 }
  171.             }
  172.  
  173.         } catch (Exception e) {
  174.             e.printStackTrace();
  175.         }
  176.         Toast.makeText(
  177.                 getBaseContext(),
  178.                 "Location changed: Lat: " + location.getLatitude() + " Lng: "
  179.                         + location.getLongitude(), Toast.LENGTH_SHORT).show();
  180.         return location;
  181.     }
  182.  
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement