gpt4 book ai didi

java - Android 中的线程和进度条?

转载 作者:行者123 更新时间:2023-12-01 11:12:27 24 4
gpt4 key购买 nike

嘿伙计们,我正在尝试显示用户和他们可以看到的卡片上某个地方之间的 GPS 距离。问题是,我认为主线程会 split 或同时做两件事。在下面的代码中,Android 在执行 if(flag) 语句中的代码时进行了 toast ...因此它会在没有获取用户坐标的情况下 toast GPS 差异...我该如何做到这一点,如果(flag) 语句首先然后继续在 if 语句之后和之外进行 toast ?

@Override
public void onResume()
{
super.onResume();
ImagePath = getIntent().getStringExtra("imagePath");
if(ImagePath == null)
{
Toast.makeText(this,"hello everyone",Toast.LENGTH_LONG).show();
}
if(newCardAdded)
{
flag = displayGpsStatus();
if(flag)
{
editLocation.setText("Please!! move your device to" +
" see the changes in coordinates." + "\nWait..");
pb.setVisibility(View.VISIBLE);
locationListener = new MyLocationListener();//LocationListener actually physically gets your location
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);


}
else
{
alertbox("Gps Status!!", "Your GPS is: OFF");
}
Global_Class.getInstance().getValue().getlatitude = place_lat;
Global_Class.getInstance().getValue().getlongitutde = place_lon;
String gps_distance = String.valueOf(gps_difference(user_lon, user_lat,place_lon,place_lat));
Toast.makeText(this, gps_distance, Toast.LENGTH_LONG).show();

}

}

最佳答案

LocationManager#requestLocationUpdates

您的 locationListener 必须实现 onLocationChanged(Location) 方法,每次位置更新都会调用该方法。然后计算距离并用该方法 toast 。由于 requestLocationUpdates(...) 在另一个线程中运行,因此无法确认它是否在 Toast.makeText(...) 之前完成,因此您必须使用您的MyLocationListener

关于java - Android 中的线程和进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32195119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com