I have a question about developing an application on Android Studio.
I want to create a function that calculates the distance when I get the location information from GPS and press the button, but I'm having trouble writing it down in Java even when I see this picture that my friend gave me.
GPS acquisition itself has been completed, and I would like to start calculating when I press the button, update every second, and output results when I finish calculating with the end button.
I would also like to know how to set the GPS acquisition interval to 1 second.
I am trying to implement the button with onButtonClick, but if it is not possible, I would like to know another way.
Below is the code in the middle of the description.Please refer to it.
(no package statement)
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.Listener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extensions AppCompatActivity{
double data1, data2;
private LocationManager locationManager;
@RequiresApi(api=Build.VERSION_CODES.M)
public void onCreate (Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRAMITION)
// TODO:consider calling
// Activity# requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[]permissions,
// int[]grantResults)
// to handle the case where the user grants the permission.See the documentation
// for Activity# requestPermissions for more details.
return;
}
locationManager.requestLocationUpdate(provider: "gps", minTimeMs: 100, minDistanceM: 0, onLocationUpdate);
}
private LocationListener onLocationUpdate=new LocationListener(){
public void onLocationChanged (Location location) {
TextView textView1=(TextView) findViewById (R.id.text_view1);
TextView textView2=(TextView) findViewById (R.id.text_view2);
// Location.getLatitude(), location.getLongitude() has the latitude and longitude of the current location.
data1 = location.getLatitude();
data2 = location.getLongitude();
String str1 = "Latitude:" + data1;
textView1.setText(str1);
// display of longitude
String str2 = "Longtude:" + data2;
textView2.setText(str2);
// Log.d("debug", "checkSelfPermission true";
}
public void onProviderDisabled (String provider) {
}
public void onProviderEnabled (String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
// I don't know from here at all ↓
public void onButtonClick (View view) {
switch(view.getId()) {
case R.id.button1:
while(1){
}
break;
}
}
public void onButtonClick (View view) {
switch(view.getId()) {
case R.id.button2:
System.out.println(new Date()+distance+"m");
break;
}
}
}
Press the Start button to start the timer at 1-second intervals.
Press the exit button to stop the timer at 1-second intervals.
A value of GPS is acquired in the processing of a timer.
I think that's the trend.
© 2024 OneMinuteCode. All rights reserved.