Guest User

Untitled

a guest
Jan 19th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. package com.example.map;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.content.Context;
  7. import android.location.Criteria;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class MapActivity extends Activity implements LocationListener{
  15. private TextView latituteField, longitudeField, accuracyField, altitudeField, bearingField;
  16. private LocationManager locationManager;
  17. private String provider;
  18.  
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_map);
  23.  
  24. latituteField = (TextView) findViewById(R.id.TextView02);
  25. longitudeField = (TextView) findViewById(R.id.TextView04);
  26. accuracyField = (TextView) findViewById(R.id.textView1);
  27. altitudeField = (TextView) findViewById(R.id.textView2);
  28. bearingField = (TextView) findViewById(R.id.textView4);
  29.  
  30. // Get the location manager
  31. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  32. // Define the criteria how to select the locatioin provider -> use default
  33. Criteria criteria = new Criteria();
  34. provider = locationManager.getBestProvider(criteria, false);
  35. Location location = locationManager.getLastKnownLocation(provider);
  36.  
  37.  
  38. // Initialize the location fields
  39. if (location != null) {
  40. System.out.println("Provider " + provider + " has been selected.");
  41. onLocationChanged(location);
  42. } else {
  43. latituteField.setText("Location not available");
  44. longitudeField.setText("Location not available");
  45. }
  46. }
  47.  
  48. /* Request updates at startup */
  49. @Override
  50. protected void onResume() {
  51. super.onResume();
  52. locationManager.requestLocationUpdates(provider, 400, 1, this);
  53. }
  54.  
  55. /* Remove the locationlistener updates when Activity is paused */
  56. @Override
  57. protected void onPause() {
  58. super.onPause();
  59. locationManager.removeUpdates(this);
  60. }
  61.  
  62. @Override
  63. public void onLocationChanged(Location location) {
  64. int lat = (int) (location.getLatitude());
  65. int lng = (int) (location.getLongitude());
  66. int acc = (int) (location.getAccuracy());
  67. int alt = (int) (location.getAltitude());
  68. int bearing = (int) (location.getBearing());
  69. latituteField.setText(String.valueOf(lat));
  70. longitudeField.setText(String.valueOf(lng));
  71. accuracyField.setText(String.valueOf(acc));
  72. altitudeField.setText(String.valueOf(alt));
  73. bearingField.setText(String.valueOf(bearing));
  74.  
  75. }
  76.  
  77. @Override
  78. public void onStatusChanged(String provider, int status, Bundle extras) {
  79. // TODO Auto-generated method stub
  80.  
  81. }
  82.  
  83. @Override
  84. public void onProviderEnabled(String provider) {
  85. Toast.makeText(this, "Enabled new provider " + provider,
  86. Toast.LENGTH_SHORT).show();
  87.  
  88. }
  89.  
  90. @Override
  91. public void onProviderDisabled(String provider) {
  92. Toast.makeText(this, "Disabled provider " + provider,
  93. Toast.LENGTH_SHORT).show();
  94. }
  95.  
  96. if(location != null){
  97. do{
  98. System.out.println("Provider " + provider + " has been selected.");
  99. onLocationChanged(location);
  100. locationManager.requestLocationUpdates(provider, 0, 0, this);
  101. }while(location != null);
  102. }
  103. else{
  104. latituteField.setText("Location not available");
  105. longitudeField.setText("Location not available");
  106. }
  107.  
  108. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  109. <uses-permission android:name="android.permission.INTERNET" />
  110.  
  111. scheduleTaskExecutor= Executors.newScheduledThreadPool(5);
  112.  
  113. // This schedule a task to run every 10 seconds:
  114.  
  115. scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
  116. public void run() {
  117.  
  118.  
  119. try {
  120.  
  121.  
  122. //add your code you would like to be executed every 10 seconds.
  123.  
  124.  
  125.  
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129.  
  130.  
  131. }
  132. }, 0, 10, TimeUnit.SECONDS);
Add Comment
Please, Sign In to add comment