Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. package pl.edu.pg.student.eti.mamlab3_155134;
  2.  
  3.  
  4. import android.Manifest;
  5. import android.annotation.SuppressLint;
  6. import android.content.Context;
  7. import android.content.pm.PackageManager;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.TextView;
  17.  
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.OutputStreamWriter;
  21.  
  22. public class MainActivity extends AppCompatActivity implements LocationListener {
  23.  
  24. LocationManager _locationManager;
  25. Location _currentLocation;
  26. Location _savedLocation;
  27. Location _previousLocation;
  28. TextView _textView;
  29.  
  30. @SuppressLint("MissingPermission")
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. _textView = findViewById(R.id.text);
  36. _locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  37. if (_locationManager != null) {
  38. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  39. System.out.println();
  40. return;
  41. }
  42.  
  43. _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  44. }
  45. }
  46.  
  47. @Override
  48. public void onLocationChanged(Location location) {
  49. _currentLocation = location;
  50. }
  51.  
  52. @Override
  53. public void onStatusChanged(String s, int i, Bundle bundle) {
  54. System.out.println("Status Changed: " + s);
  55. }
  56.  
  57. @Override
  58. public void onProviderEnabled(String s) {
  59.  
  60. }
  61.  
  62. @Override
  63. public void onProviderDisabled(String s) {
  64.  
  65. }
  66.  
  67. public void buttonOnClick(View view) {
  68. if(_savedLocation != null)
  69. _previousLocation = _savedLocation;
  70. else _previousLocation = _currentLocation;
  71.  
  72. _savedLocation = _currentLocation;
  73.  
  74. String s = "Current latitude" +
  75. Double.toString(_currentLocation.getLatitude()) +
  76. "\nPrevious latitude" +
  77. Double.toString(_previousLocation.getLatitude()) +
  78. "\nDiference of latidude" +
  79. Double.toString(_currentLocation.getLatitude() - _previousLocation.getLatitude())+
  80. "\nCurrent longitude" +
  81. Double.toString(_currentLocation.getLongitude()) +
  82. "\nPrevious longitude" +
  83. Double.toString(_previousLocation.getLongitude()) +
  84. "\nDiference of longitude" +
  85. Double.toString(_currentLocation.getLongitude() - _previousLocation.getLongitude()) +
  86. "\nCurrent bearing" +
  87. Double.toString(_currentLocation.getBearing()) +
  88. "\nPrevious bearing" +
  89. Double.toString(_previousLocation.getBearing()) +
  90. "\nDiference of bearing" +
  91. Double.toString(_currentLocation.getBearing() - _previousLocation.getBearing()) +
  92. "\nCurrent accuracy" +
  93. Double.toString(_currentLocation.getAccuracy()) +
  94. "\nPrevious accuracy" +
  95. Double.toString(_previousLocation.getAccuracy()) +
  96. "\nDiference of accuracy" +
  97. Double.toString(_currentLocation.getAccuracy() - _previousLocation.getAccuracy()) +
  98. "\nCurrent speed" + _currentLocation.getSpeed();
  99.  
  100. writeToFile(s, this);
  101. _textView.setText(s);
  102. }
  103.  
  104. private void writeToFile(String data, Context context) {
  105. try {
  106. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
  107. outputStreamWriter.write(data);
  108. outputStreamWriter.close();
  109. }
  110. catch (IOException e) {
  111. Log.e("Exception", "File write failed: " + e.toString());
  112. }
  113. }
  114.  
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement