当前位置: 移动技术网 > 移动技术>移动开发>Android > android studio 53 mp3

android studio 53 mp3

2020年08月10日  | 移动技术网移动技术  | 我要评论
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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网