是我遇到这个问题的日子。我创建了一个带有片段和标签的应用程序,我必须插入一个按钮,当点击它连接到某个IP和某个端口时,我不知道如何编写以及如何设置它们,它们只能在连接时才能连接它启动mainActivity,但我希望可以通过按钮控制它,你能帮忙吗?
答案 0 :(得分:0)
首先,对于最新版本的Android框架,无法从主线程(即管理UI的线程)执行所有网络操作。因此,您需要使用自己的线程来建立连接。
一个例子:
package it.resis.solarapp.activities.fourcloud.application;
import it.resis.solarapp.R;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CopyOfActivity4CloudConfiguration extends Activity {
private volatile Socket socket = null;
private boolean connectionOk = false;
private Button buttonDisconnect;
private Button buttonConnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
buttonConnect = findViewById(R.id.buttonConnect);
buttonConnect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
InetSocketAddress addr = new InetSocketAddress("192.168.1.1", 80);
new ConnectToIpTask().execute(addr);
}
});
buttonDisconnect = findViewById(R.id.buttonDisconnect);
buttonConnect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new CloseSocket().execute();
}
});
buttonDisconnect.setEnabled(false);
buttonConnect.setEnabled(true);
}
private class ConnectToIpTask extends AsyncTask<InetSocketAddress, Void, Boolean> {
@Override
protected Boolean doInBackground(InetSocketAddress... params) {
InetSocketAddress addr = params[0];
try {
socket = new Socket(addr.getAddress().toString(), addr.getPort());
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
connectionOk = result;
// update here the ui with result
if (result) {
buttonDisconnect.setEnabled(true);
buttonConnect.setEnabled(false);
}
}
}
private class CloseSocket extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
socket.close();
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
buttonDisconnect.setEnabled(false);
buttonConnect.setEnabled(true);
}
}
}
答案 1 :(得分:0)
我可以,我改变了代码行 从:
@Override
protected Boolean doInBackground(InetSocketAddress... params) {
InetSocketAddress addr = params[0];
try {
**socket = new Socket(addr.getAddress().toString(), addr.getPort());**
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
到
@Override
protected Boolean doInBackground(InetSocketAddress... params) {
InetSocketAddress addr = params[0];
try {
**socket = new Socket("192.168.1.1", 80);**
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
感谢所有