Android 位置和线程

发布于 2024-10-06 14:04:26 字数 2507 浏览 1 评论 0原文

我正在尝试大量使用 Location API。我做一个用 LocationListener 刷新 TextView 的简单 Activity 没有任何问题,这很好。

问题是,我必须将所有位置内容放入类似 Manager 类之类的东西中,该类由主 Activity 调用。所以我必须将所有内容放在该类中并将位置传递给活动。这是活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    g = new GPSManager(getApplicationContext());
    t = new Thread(g);
    t.start();
    updateWithNewLocation(g.getLocation());
}    
private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.loca);
    if (location != null) {
       double lat = location.getLatitude();
       double lng = location.getLongitude();
       latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
       latLongString = "No location found";
    }
    myLocationText.setText("Your Current Position is:\n" +
    latLongString);
 }    

这是“Manager”类:

public GPSManager(Context context) {
    locationManager = (LocationManager)context.getSystemService(service); 
    String service = Context.LOCATION_SERVICE;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true); 
}
public Location getLocation() {
    return this.location;
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        Manager.this.location=location;
    }
    public void onProviderDisabled(String provider){
        locationManager.removeUpdates(this);
    }
    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status,
        Bundle extras){ }
    };

@Override
public void run() {
   Looper.prepare();
   locationManager.requestLocationUpdates(provider, 0, 0,
           locationListener);        
   location = locationManager.getLastKnownLocation(provider);
   Looper.loop();
   Looper.myLooper().quit();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
    locationManager.removeUpdates(locationListener);
}

我遇到的问题是,它根本不起作用!如果我将此代码放入活动中,不涉及任何线程,效果会很好。但这样,它只是说“找不到位置”。我需要让 Manager 类始终监听更改,因此当我请求当前位置时会更新。

i'm trying to make heavy use of the Location API. I have no problem whatsoever doing a simple Activity that refreshes a TextView with a LocationListener, that's fine.

The thing is, i must put all the Location stuff in something like a Manager class that gets called by the main Activity. So i must place everything inside that class and pass say the location to the Activity. This is the Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    g = new GPSManager(getApplicationContext());
    t = new Thread(g);
    t.start();
    updateWithNewLocation(g.getLocation());
}    
private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.loca);
    if (location != null) {
       double lat = location.getLatitude();
       double lng = location.getLongitude();
       latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
       latLongString = "No location found";
    }
    myLocationText.setText("Your Current Position is:\n" +
    latLongString);
 }    

And this is the "Manager" class:

public GPSManager(Context context) {
    locationManager = (LocationManager)context.getSystemService(service); 
    String service = Context.LOCATION_SERVICE;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true); 
}
public Location getLocation() {
    return this.location;
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        Manager.this.location=location;
    }
    public void onProviderDisabled(String provider){
        locationManager.removeUpdates(this);
    }
    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status,
        Bundle extras){ }
    };

@Override
public void run() {
   Looper.prepare();
   locationManager.requestLocationUpdates(provider, 0, 0,
           locationListener);        
   location = locationManager.getLastKnownLocation(provider);
   Looper.loop();
   Looper.myLooper().quit();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
    locationManager.removeUpdates(locationListener);
}

The problem i have is that, well, it doesn't works at all! If i put this code in the activity, no thread involved, it works great. But this way, it just says "no location found". I need to make the Manager class always listening to changes, so when i ask for the current location is updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-10-13 14:04:26

选项#1:删除“manager”类。

选项#2:使“manager”类成为一个Service

选项#3:使“manager”类成为自定义Application

选项#4:将Thread 替换为HandlerThread

Option #1: Delete the "manager" class.

Option #2: Make the "manager" class be a Service.

Option #3: Make the "manager" class be a custom Application.

Option #4: Replace the Thread with a HandlerThread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文