安卓实现点击Button和滑动SeekBar发送数据到服务端

第一次发播客,我都不晓得怎么发,由于本人也是从小白自学过来的,现在还算能自己搞点东西吧,希望下面写的代码能给各位初学者一点帮助。

 以下这段安卓程序是由Button和SeekBar组成,点击Button启动线程,线程里面启动Socket,通过Socket把已经定义好Button里面的数据及命令发送到服务端,通过滑动SeekBar,SeekBar每滑动一次,百分比每增加一次,就发送指定命令到服务端。

希望能为各位带来一点帮助,就算个小小初级程序员,写的不好,希望各位能多多指教。

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnSeekBarChangeListener,
OnClickListener {

SeekBar seekBar1;
SeekBar seekBar2;
TextView textViewSet4, textViewSet2;
Button button1_1, button1_2, button2_1, button2_2, button3_1, button3_2;
Socket socket = null;
String str;
String s1;
int i;
Timer t;
// String buffer = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
    seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
    textViewSet4 = (TextView) findViewById(R.id.textViewSet4);
    textViewSet2 = (TextView) findViewById(R.id.textViewSet2);
    button1_1 = (Button) findViewById(R.id.button1_1);
    button1_2 = (Button) findViewById(R.id.button1_2);
    button2_1 = (Button) findViewById(R.id.button2_1);
    button2_2 = (Button) findViewById(R.id.button2_2);
    button3_1 = (Button) findViewById(R.id.button3_1);
    button3_2 = (Button) findViewById(R.id.button3_2);

    seekBar1.setOnSeekBarChangeListener(this);
    seekBar2.setOnSeekBarChangeListener(this);
    button1_1.setOnClickListener(this);
    button1_2.setOnClickListener(this);
    button2_1.setOnClickListener(this);
    button2_2.setOnClickListener(this);
    button3_1.setOnClickListener(this);
    button3_2.setOnClickListener(this);

}

@Override
public synchronized void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {// 滑竿移动
    switch (seekBar.getId()) {
    case R.id.seekBar1:
        textViewSet2.setText(progress + "%");
        seekBar1.setMax(100);
        i = seekBar1.getProgress();
        Log.e("TAG", "亮度值 :" + i);
        s1 = Integer.toHexString(i);
        Log.e("TAG", "十六进制数 :" + s1);

//// new Yy(s1).start();
// t = new Timer();
// t.schedule(new Yy(s1), 1000);
// Log.e(“TAG”, “多久发一次:” + t);

        break;

    case R.id.seekBar2:
        textViewSet4.setText(progress + "%");
        seekBar1.setMax(100);
        i = seekBar1.getProgress();
        Log.e("TAG", "亮度值 :" + i);
        s1 = Integer.toHexString(i);
        Log.e("TAG", "十六进制数 :" + s1);
        break;
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {// 按下滑竿
    t = new Timer();
    t.schedule(new Yy(s1), 1000);  
}


@Override
public void onStopTrackingTouch(SeekBar seekBar) {// 离开滑竿
    t = new Timer();
    t.schedule(new Yy(s1), 1000);  
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1_1:
        Toast.makeText(this, "正在左转", 0).show();

        byte b[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X31, 0X30, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b).start();

        break;

    case R.id.button1_2:
        Toast.makeText(this, "正在右转", 0).show();
        byte b1[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X31, 0X31, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b1).start();
        break;

    case R.id.button2_1:
        Toast.makeText(this, "正在左转", 0).show();
        byte b2[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X32, 0X30, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b2).start();
        break;

    case R.id.button2_2:
        Toast.makeText(this, "正在左转", 0).show();
        byte b3[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X32, 0X31, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b3).start();
        break;

    case R.id.button3_1:
        Toast.makeText(this, "正在左转", 0).show();
        byte b4[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X33, 0X30, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b4).start();
        break;

    case R.id.button3_2:
        Toast.makeText(this, "正在左转", 0).show();
        byte b5[] = { 0X02, 0X31, 0X30, 0X30, 0X30, 0X31, 0X33, 0X31, 0X30,
                0X03 };
        // 启动线程 向服务器发送和接收信息
        new MyThread(b5).start();
        break;
    }
}

}

class Yy extends TimerTask{
public String s1;

public Yy(String s1) {
    this.s1 = s1;
}

public void run() {
    try {

        Socket socket1 = new Socket("192.168.8.102", 52358);
        OutputStream os1 = socket1.getOutputStream();
        PrintStream ps = new PrintStream(os1);
        ps.print(s1);

        ps.close();
        os1.close();
        socket1.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

class MyThread extends Thread {
public byte b[];

public MyThread(byte b[]) {
    this.b = b;
}

@Override
public void run() {
    // // 定义消息
    // Bundle bundle = new Bundle();
    // bundle.clear();
    try {
        Socket socket = new Socket("192.168.8.102", 52358);
        OutputStream os = socket.getOutputStream();
        os.write(b);

        os.close();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

发布了1 篇原创文章 · 获赞 1 · 访问量 419
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览