java - 根据我当前的位置每秒更新 TextView

标签 java android android-studio

我使用 google play-services-location 来获取当前位置并使用函数 getLocation() 显示纬度和经度。

纬度和经度是两个不同的TextView

但现在我想每秒更新纬度和经度,但有一些问题。

我一直在尝试使用线程类,但没有得到结果。

public class MainActivity extends AppCompatActivity {

 public String latitude, longitude;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    requestPermission();

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            getLocation();
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    thread.start();


}

private void requestPermission(){
    ActivityCompat.requestPermissions(this,new String[]{ACCESS_FINE_LOCATION},1);
}

public void getLocation(){

    FusedLocationProviderClient
        client = LocationServices.getFusedLocationProviderClient(this);

        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        client.getLastLocation().addOnSuccessListener(MainActivity.this,new OnSuccessListener<Location>(){

                    @Override
                    public void onSuccess(Location location) {

                        if(location!=null){

                            latitude = Double.toString(location.getLatitude());
                            TextView textView = findViewById(R.id.latitude_text);
                            textView.setText( "Latitude :- " + latitude );

                            longitude = Double.toString(location.getLongitude());
                            textView = findViewById(R.id.longitude_text);
                            textView.setText("Longitude :- " + longitude );

                        }
                    }
                }
        );

 }
}

最佳答案

您可以在 getLocation() 方法末尾使用 HandlerpostDelayed 方法,如下所示

  handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getLocation();
            }
        }, 1000);

关于java - 根据我当前的位置每秒更新 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745940/

相关文章:

java - 在 Android 开发中如何绘制矩阵?

java - 无法将日期从字符串写入 TextView

react-native - React Native - 按下按钮 "NativeAnimatedModule.startOperationBatch is not a function"

java - 强制 Eclipse 仅使用 Java 1.4

java - 禁用后对 jButton 执行的操作

java - apply stream 过滤掉所有满足条件的元素,除了一个

android - 在使用 AndroidX LiveData 和模拟 View 模型进行仪器化测试时遇到问题

java - 为什么在制作 FragmentPagerAdapter 时出现错误?

android-studio - 迁移到新 Places SDK 客户端

java - 如何使用正则表达式匹配字符串