【发布时间】:2021-04-27 18:20:33
【问题描述】:
autoSearch_button.setOnClickListener(view -> {
grant_permission();
check_location_enableornot();
lastButton_pressed = view.getId();
ExampleRunnable exampleRunnable = new ExampleRunnable(20);
Thread thread = new Thread(exampleRunnable);
thread.start();
thread.join();
});
public class ExampleRunnable implements Runnable {
private int kmRadius;
private double lat =0 , longi =0 ;
public ExampleRunnable(int i) {
this.kmRadius = i;
}
@Override
public void run() {
lastLocation();
}
private void lastLocation(){
Log.i(TAG, "lastLocation: " + Thread.currentThread().getName());
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
// for now being assume that i have declared
@SuppressLint("MissingPermission") Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
locationTask.addOnSuccessListener(location -> {
if (location != null) {
//We have a location
Log.d(TAG, "run: last location" + Thread.currentThread().getName()););
this.lat = location.getLatitude();
this.longi = location.getLongitude();
print();
} else {
Log.d(TAG, "onSuccess: Location was null... calling robust");
}
}).addOnFailureListener(e -> Log.e(TAG, "onFailure: " + e.getLocalizedMessage() ));
}
}
public synchronized void print(){
Log.d(TAG, "print: " + Thread.currentThread.getName());
}
}
在 logcat 中输出我想要的内容
- 最后一个位置:thread4
- 运行:lastLocation thread4
- 打印:thread4
但是我得到了什么结果- 最后位置:线程4 // 运行:lastLocation main // 打印:主要
我想在特定线程中处理位置并对其进行处理
【问题讨论】:
标签: java android multithreading performance location