我有一个位置侦听器在服务类内部的单独线程上运行,如下所示:
public void onCreate() {
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
final Criteria locCriteria = new Criteria();
locCriteria.setPowerRequirement(Criteria.POWER_LOW);
locCriteria.setAccuracy(Criteria.ACCURACY_FINE);
locCriteria.setSpeedRequired(true);
locThread = new Thread(new Runnable() {
@Override
public void run() {
provider = locationManager.getBestProvider(locCriteria, true);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.i("Lat changed", String.valueOf(latitude));
Log.i("Lon changed", String.valueOf(longitude));
}我如何将协助表发送回主activity类,以便在文本视图中显示它们?除了使用广播之外。
发布于 2018-04-06 02:59:33
您可以将LocationListener的实例传递给您的服务,而不是在那里实现侦听器。例如:
public class MyService extends Service {
private LocationListener locationListener;
public MyService(LocationListener locationListener) {
this.locationListener = locationListener;
}
@Override
public void onCreate() {
// Do your stuff
// add this location listener where it needs to be
}
}然后,在您的活动或片段中,实现LocationListener并按照您的意愿处理结果。
https://stackoverflow.com/questions/49678841
复制相似问题