Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.edu.pg.student.eti.mamlab3_155134;
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- public class MainActivity extends AppCompatActivity implements LocationListener {
- LocationManager _locationManager;
- Location _currentLocation;
- Location _savedLocation;
- Location _previousLocation;
- TextView _textView;
- @SuppressLint("MissingPermission")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- _textView = findViewById(R.id.text);
- _locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- if (_locationManager != null) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- System.out.println();
- return;
- }
- _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
- }
- }
- @Override
- public void onLocationChanged(Location location) {
- _currentLocation = location;
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- System.out.println("Status Changed: " + s);
- }
- @Override
- public void onProviderEnabled(String s) {
- }
- @Override
- public void onProviderDisabled(String s) {
- }
- public void buttonOnClick(View view) {
- if(_savedLocation != null)
- _previousLocation = _savedLocation;
- else _previousLocation = _currentLocation;
- _savedLocation = _currentLocation;
- String s = "Current latitude" +
- Double.toString(_currentLocation.getLatitude()) +
- "\nPrevious latitude" +
- Double.toString(_previousLocation.getLatitude()) +
- "\nDiference of latidude" +
- Double.toString(_currentLocation.getLatitude() - _previousLocation.getLatitude())+
- "\nCurrent longitude" +
- Double.toString(_currentLocation.getLongitude()) +
- "\nPrevious longitude" +
- Double.toString(_previousLocation.getLongitude()) +
- "\nDiference of longitude" +
- Double.toString(_currentLocation.getLongitude() - _previousLocation.getLongitude()) +
- "\nCurrent bearing" +
- Double.toString(_currentLocation.getBearing()) +
- "\nPrevious bearing" +
- Double.toString(_previousLocation.getBearing()) +
- "\nDiference of bearing" +
- Double.toString(_currentLocation.getBearing() - _previousLocation.getBearing()) +
- "\nCurrent accuracy" +
- Double.toString(_currentLocation.getAccuracy()) +
- "\nPrevious accuracy" +
- Double.toString(_previousLocation.getAccuracy()) +
- "\nDiference of accuracy" +
- Double.toString(_currentLocation.getAccuracy() - _previousLocation.getAccuracy()) +
- "\nCurrent speed" + _currentLocation.getSpeed();
- writeToFile(s, this);
- _textView.setText(s);
- }
- private void writeToFile(String data, Context context) {
- try {
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
- outputStreamWriter.write(data);
- outputStreamWriter.close();
- }
- catch (IOException e) {
- Log.e("Exception", "File write failed: " + e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement