我正在製作一個接收UDP消息的應用程序。我遇到的問題是顯示Activity
,因爲它僅在收到UDP
消息後才顯示。在onCreate
我有startUdp()
它開始收聽UDP
消息,我認爲這是問題。直到收到UDP數據包纔會顯示活動
有沒有辦法告訴Activity
何時完成加載或我應該開始收聽?
我Activity
代碼:
public class UDPActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_UDP);
// Setup the UDP stuff
startUDP();
System.out.println("Sent Response of ");
TextView rowLetter = (TextView) findViewById(R.id.rowLetter);
TextView seatNumber = (TextView) findViewById(R.id.seatNumber);
Button btnClose = (Button) findViewById(R.id.btnClose);
Intent i = getIntent();
// Binding Click event to Button
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_make_light, menu);
return true;
}
private static final int UDP_SERVER_PORT = 12345;
private static final int MAX_UDP_DATAGRAM_LEN = 1500;
private void startUDP() {
Log.d("UDP", "S: Connecting...");
String lText;
byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN];
DatagramSocket ds = null;
while (true) {
try {
ds = new DatagramSocket(UDP_SERVER_PORT);
//disable timeout for testing
//ds.setSoTimeout(100000);
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
Log.d("UDP", "S: Receiving...");
ds.receive(dp);
lText = new String(lMsg, 0, dp.getLength());
Log.i("UDP packet received", "S: Recieved '" + lText);
textView = (TextView) findViewById(R.id.text1);
textView.setText(lText);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
}
}
爲了澄清您的問題請發佈代碼! – pedr0 2012-07-11 15:59:09
'startUdp()'塊嗎? – 2012-07-11 16:01:26
您應該修復您的問題以獲得更好的格式。但是,您可以使用Log命令查看代碼已經存在的位置。 'Log.e(「UDP」,「+++ AFTER STARTUDP +++」);'在startUdp()調用之後,如果程序到達那行代碼,它將出現在你的LogCat中。 – JuiCe 2012-07-11 16:06:06