欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android studio 53 mp3

程序员文章站 2022-06-24 20:09:24
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();
    }
    
}






android studio 53 mp3

本文地址:https://blog.csdn.net/weixin_33595571/article/details/107884052