android聊天+app代码,用sockets打造自己的Android聊天app(安卓篇)
时间: 2023-12-11 20:04:35 浏览: 43
很高兴能为您解答这个问题。以下是一个简单的Android聊天应用程序的代码,使用Sockets实现:
1. 创建项目并添加依赖项:
```
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
```
2. 创建界面布局文件:
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ems="10" />
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentEnd="true"
android:text="Send" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/editText">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
```
3. 创建后台线程和Socket:
MainActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private Socket socket;
private PrintWriter printWriter;
private BufferedReader bufferedReader;
private TextView textView;
private EditText editText;
private Button sendButton;
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
sendButton = findViewById(R.id.sendButton);
scrollView = findViewById(R.id.scrollView);
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket("your_server_ip_address", 1234);
printWriter = new PrintWriter(socket.getOutputStream(), true);
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
textView.append(message.obj.toString());
scrollView.fullScroll(View.FOCUS_DOWN);
}
};
String line;
while ((line = bufferedReader.readLine()) != null) {
Message message = new Message();
message.obj = line + "\n";
handler.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString();
editText.setText("");
printWriter.println(message);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
注意替换 `your_server_ip_address` 为您的服务器IP地址。
4. 启动服务器并测试应用程序:
使用另一个设备或模拟器作为服务器,并在运行之前启动服务器。启动应用程序并测试聊天功能。
希望这可以帮助您开始使用Sockets构建Android聊天应用程序。