android studio 53 mp3
程序员文章站
2022-03-21 22:26:23
package com.example.mydownloadmusic01;import android.annotation.SuppressLint;import android.os.Handler;import android.os.Message;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;...
package com.example.mydownloadmusic01;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
TextView tv;
ListView ls;
Button btn;
EditText edt;
String NeedFindWords;
private final int HANDLER_MSG_TELL_RECV = 0x124;
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
public void handleMessage(Message msg){
//接受到服务器信息时执行
// Toast.makeText(MainActivity.this,(msg.obj).toString(),Toast.LENGTH_LONG).show();
// tv.setText((msg.obj).toString());
System.out.println((msg.obj).toString());
String[] strArr = (msg.obj).toString().split("\n");
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String> (
MainActivity.this, android.R.layout.simple_list_item_1,strArr);
ls.setAdapter(arrayAdapter);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button);
edt=(EditText)findViewById(R.id.editText);
ls= (ListView) findViewById(R.id.ListName);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(id);
Toast.makeText(MainActivity.this,"rrrrrrrrrrrrrrrrrr",Toast.LENGTH_LONG).show();
}
});
// tv= (TextView) findViewById(R.id.ReData);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 响应事件
NeedFindWords=edt.getText().toString();
startNetThread();
}
});
}
private void startNetThread() {
new Thread() {
@Override
public void run() {
try {
Socket socket = new Socket("192.168.43.8", 666);
InputStream is = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(NeedFindWords.getBytes()); //3.发送
out.flush();
byte[] bytes = new byte[1024];
int n = is.read(bytes);
Message msg = handler.obtainMessage(HANDLER_MSG_TELL_RECV, new String(bytes, 0, n));
msg.sendToTarget();
is.close();
socket.close();
} catch (Exception e) {
}
}
}.start();
}
}
本文地址:https://blog.csdn.net/weixin_33595571/article/details/107884052
上一篇: Android实现Z轴布局效果
推荐阅读
-
Android编程实现播放MP3功能示例
-
Android studio点击跳转WebView详解
-
Mac OS下为Android Studio编译FFmpeg解码库的详细教程
-
Mac Android Studio快捷键整理
-
Android Studio中快捷键实现try catch等功能包含代码块的实现方法
-
Android Studio生成函数注释的实现方法
-
Android Studio导入so文件到项目中的实例详解
-
详解Android studio ndk配置cmake开发native C
-
android studio集成ijkplayer的示例代码
-
Android studio so库找不到问题解决办法