positioninglistener.java
来自「主要用于基站定位的java实现」· Java 代码 · 共 82 行
JAVA
82 行
/*
* PositioningListener.java
*
*/
package com.sonyericsson.example;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
/**
*
*/
public class PositioningListener implements LocationListener {
private Location currentLocation = null;
private int currentState = 0;
private static final Object STATE_LOCK = new Object();
private static final Object LOCATION_LOCK = new Object();
private boolean locationUpdated = false;
private boolean stateUpdated = false;
public PositioningListener() {
}
public String getStateString() {
return Utils.getStateString(currentState);
}
public Location getCurrentLocation() {
synchronized (LOCATION_LOCK) {
return currentLocation;
}
}
public Location waitForLocation() {
synchronized (LOCATION_LOCK) {
try {
while (!locationUpdated) {
LOCATION_LOCK.wait();
}
locationUpdated = false; // reset flag
} catch (InterruptedException i) {
i.printStackTrace();
}
return currentLocation;
}
}
public int waitForStateChange() {
synchronized (STATE_LOCK) {
try {
while (!stateUpdated) {
STATE_LOCK.wait();
}
stateUpdated = false; // reset flag
} catch (InterruptedException i) {
i.printStackTrace();
}
}
return currentState;
}
public void providerStateChanged(LocationProvider locationProvider, int i) {
synchronized (STATE_LOCK) {
currentState = i;
stateUpdated = true;
STATE_LOCK.notifyAll();
}
}
public void locationUpdated(LocationProvider locationProvider, Location location) {
synchronized (LOCATION_LOCK) {
currentLocation = location;
locationUpdated = true;
LOCATION_LOCK.notifyAll();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?