App概述:在端口上监听连接的简单服务器。对于每个连接到该端口的客户端,当它们从GPS芯片中取出时,它会发送GPS字符串。
因此,onCreate启动一个在端口侦听的新线程。对于每个连接,此侦听器线程派生出另一个新线程。目前,这些线程中的每两个线程每隔两秒钟就会发出一条“仍然连接”的消息。这部分全部工作,我可以设置一对电信会话和连接,一切如预期。在这段代码中,所有的位置信息都在工作;它可以正确地更新屏幕上的GPS信息,并进行正确的更改。
我想注册一个Nmealistener,然后让它将该字符串分派给每个为连接服务的线程。但是,socketListenerThread.MasterDispatchNmeaMessage(nmea)不会在addNmeaListener内部解决问题。(它也解决不了这个问题。)addNmeaListener似乎没有问题,如果它只是通过Log.d转储消息,它会很高兴地做到这一点。
在socketListenerThread类中,方法MasterDispatchNmeaMessage也被标记为“从不调用”。
,那么是什么阻止了MasterDispatchNmeaMessage的解决呢?
对于那些不熟悉的人,示例NMEA字符串如下所示:
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47在我正在测试的三星S4上,它们可能会以每秒5到10行的速度出现。
附加注意:我的目标是API 22。在此之后,更多的权限检查将用于location类。因此,在添加新的权限检查以使其与最新的库兼容之前,我希望所有这些都能实际工作。
public class ActivityMain extends AppCompatActivity implements LocationListener {
static final String TAG = "GPSOverWiFi";
static final int LISTENING_PORT = 8085;
private TextView latitudeField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
Thread socketListenerThread;
TextView info, infoip;
ServerSocket serverSocket;
int gConnectionCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeField = (TextView) findViewById(R.id.liveLatitude);
longitudeField = (TextView) findViewById(R.id.liveLongitude);
infoip = (TextView) findViewById(R.id.infoip);
info = (TextView) findViewById(R.id.info);
// Get the GPS location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latitudeField.setText("Location not available");
longitudeField.setText("Location not available");
}
infoip.setText("IP Addresss: " + getIpAddress() + ":" + LISTENING_PORT);
socketListenerThread = new SocketListenerThread();
socketListenerThread.start();
//for giggles, it doesn't resolve here either, but this it needs to resolve a few lines down...
//socketListenerThread.MasterDispatchNmeaMessage("asdf");
locationManager.addNmeaListener(new GpsStatus.NmeaListener() {
public void onNmeaReceived(long timestamp, String nmea) {
// ////////////////////////////////////////////////////////////
// socketListenerThread.MasterDispatchNmeaMessage errors here with 'can't resolve
// ////////////////////////////////////////////////////////////
socketListenerThread.MasterDispatchNmeaMessage(nmea);
Log.d(TAG, "NMEA: " + nmea);
}
});
}
public class SocketListenerThread extends Thread {
Socket socket;
ArrayList<EachConnectionThread> connectionThreads = new ArrayList<EachConnectionThread>();
public void run() {
try {
serverSocket = new ServerSocket(LISTENING_PORT);
while (!isInterrupted()) { //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
socket = serverSocket.accept();
gConnectionCount++;
EachConnectionThread thread = new EachConnectionThread(socket);
connectionThreads.add(thread);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void MasterDispatchNmeaMessage(String nmeaMessage) {
for (EachConnectionThread serverThread : connectionThreads)
serverThread.dispatchNmeaMessage(nmeaMessage);
}
}
public class EachConnectionThread extends Thread {
private Socket hostThreadSocket;
EachConnectionThread(Socket socket) {
hostThreadSocket = socket;
}
PrintStream printStream;
@Override
public void run() {
try {
OutputStream outputStream;
outputStream = hostThreadSocket.getOutputStream();
printStream = new PrintStream(outputStream);
printStream.print("Connected\r\n");
while(!printStream.checkError()) {
SystemClock.sleep(2000);
printStream.print("Still connected\r\n");
}
printStream.close();
outputStream.close();
hostThreadSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
gConnectionCount--;
}
public void dispatchNmeaMessage(String nmeaString){
printStream.print(nmeaString);
}
}发布于 2015-11-30 20:37:50
更改成员变量声明
Thread socketListenerThread;至
SocketListenerThread socketListenerThread;因此,您可以通过SocketListenerThread实例访问socketListenerThread方法。
https://stackoverflow.com/questions/34007511
复制相似问题