我有3个按钮可以使用wifi打开不同的LED。我想要做的是在按下按钮时启动一个线程。该线程将执行一个网络内容,即发送一串指令代码。但当我按下按钮时,它停止运行。如果发生异常,我想在吐司中显示异常。请帮忙。 这是代码:
这是将显示3个按钮的活动。
package com.gfhz.appliancecontrol;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class LightsMenuActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lights_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_lights_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void checkLightsStatus() {
/*instructionID = "0400";
Intent intent = new Intent(this, ConnectionSocket.class);
intent.putExtra(INSTRUCTION, instructionID);
startActivity(intent);*/
}
public void powerLamp1 (View view) {
ConnectionSocket cs = new ConnectionSocket();
cs.sendInstruction("0401");
}
public void powerLamp2(View view) {
ConnectionSocket cs = new ConnectionSocket();
cs.sendInstruction("0402");
}
public void powerLamp3(View view){
ConnectionSocket cs = new ConnectionSocket();
cs.sendInstruction("0403");
}
}
这是将执行该线程的活动。
package com.gfhz.appliancecontrol;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.widget.Toast;
public class ConnectionSocket extends Activity{
Socket client;
ConnectionInfoActivity conInfo;
String instructionCode;
String serverAddr;
OutputStream os;
DataOutputStream dos;
String stoast;
public void sendInstruction (String instruction) {
// Get the DEVICE ID
String devId = conInfo.getDevId();
// Concatenate instruction message and device Id
instructionCode = devId.concat(instruction);
// Get the SERVER IP ADDRESS
serverAddr = "192.168.1.105";
// Initialize PORT
final int port = 2000;
new Thread (new Runnable() {
public void run() {
try{
// Open a socket
client = new Socket(serverAddr, port);
dos = new DataOutputStream(client.getOutputStream());
dos.writeBytes(instructionCode);
} catch (UnknownHostException e) {
//TODO Auto-generated catch block
e.printStackTrace();
stoast = "Unknown Host Exception" + e.toString();
} catch (IOException e) {
// TODO Auto-generated cach block
e.printStackTrace();
// Display in TOAST
stoast = "IO Exception" + e.toString();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
Toast.makeText(getApplicationContext(), stoast, Toast.LENGTH_SHORT).show();
}
}