一直都好使啊。。好使 好使

MainActivity://overover

package com.example.sockettest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

import com.example.sockettest.DataService.CarRecordBinder;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

Socket socket = null;
    String buffer = "";
    public String line ;
    TextView txt1;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    public static DataService mService;
    private String TAG = "";

TextView textview;
    String geted1;
    BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
        public void onReceive(Context context, Intent intent) {
            line = mService.getLine();
            textview.setText(line) ;
        }
    };

public Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x11) {

// txt1.append("server:"+bundle.getString("msg")+"\n");

Toast.makeText(MainActivity.this, "发送成功! ", Toast.LENGTH_LONG)
                        .show();
            }
        }

};

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.app1);
        btn1.setOnClickListener(this);
        btn2 = (Button) findViewById(R.id.app2);
        btn2.setOnClickListener(this);
        btn3 = (Button) findViewById(R.id.app3);
        btn3.setOnClickListener(this);
        btn4 = (Button) findViewById(R.id.app4);
        btn4.setOnClickListener(this);

textview = (TextView) findViewById(R.id.textview);

Intent i = new Intent("com.neusoft.radioserver.RadioGpsService");
        bindService(i, mConnection, Context.BIND_AUTO_CREATE);

IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.dede.dede");
        registerReceiver(receiver, intentFilter);

}

class MyThread extends Thread {

public String txt1;

public MyThread(String str) {
            txt1 = str;
        }

@Override
        public void run() {
            // 定义消息
            Message msg = new Message();
            msg.what = 0x11;
            Bundle bundle = new Bundle();
            bundle.clear();
            try {
                // 连接服务器 并设置连接超时为5秒
                socket = new Socket();
                socket.connect(new InetSocketAddress("1.1.9.30", 30000), 5000);
                // 获取输入输出流
                OutputStream ou = socket.getOutputStream();
                BufferedReader bff = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                // 读取发来服务器信息
                // String line = null;
                // buffer="";
                // while ((line = bff.readLine()) != null) {
                // buffer = line + buffer;
                // }

// 向服务器发送信息
                ou.write(txt1.getBytes());
                ou.flush();
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
                // 关闭各种输入输出流
                bff.close();
                ou.close();
                socket.close();
            } catch (SocketTimeoutException aa) {
                // 连接超时 在UI界面显示消息
                bundle.putString("msg", "服务器连接失败!请检查网络是否打开");
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.app1:
            // 启动线程 向服务器发送和接收信息
            new MyThread("APP1").start();
        case R.id.app2:
            // 启动线程 向服务器发送和接收信息
            new MyThread("APP2").start();
        case R.id.app3:
            // 启动线程 向服务器发送和接收信息
            new MyThread("APP3").start();
        case R.id.app4:
            // 启动线程 向服务器发送和接收信息
            new MyThread("APP4").start();
        }

}

private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = ((CarRecordBinder) service).getService();
 
        }

public void onServiceDisconnected(ComponentName className) {
            Log.d(TAG, "disconnect service");
            mService = null;
        }
    };

}

dssfsfsfs的更多相关文章

随机推荐

  1. C语言回调函数详解

    1. 什么是回调函数? 回调函数,光听名字就比普通函数要高大上一些,那到底什么是回调函数呢?恕我读得书少,没有在那本书上看到关于回调函数的定义.我在百度上搜了一下,发现众说纷纭,有很大一部分都是使用类 ...

  2. Spring Batch介绍

    简介 SpringBatch 是一个大数据量的并行处理框架.通常用于数据的离线迁移,和数据处理,⽀持事务.并发.流程.监控.纵向和横向扩展,提供统⼀的接⼝管理和任务管理;SpringBatch是Spr ...

  3. 实例甜点 Unreal Engine 4迷你教程(3)之用C++改变Image小部件的其它属性

    完成本迷你教程之前,请前往完成以下迷你教程: ·实例甜点 Unreal Engine 4迷你教程(2)之用C++改变Image小部件的颜色: 在上一次的迷你教程的LearnWidgets工程上进行(如 ...

  4. 子查询语句的thinkphp实现

    语句 SELECT a.id as item_id,a.name as item_name,a.intro as item_intro,b.id,b.money FROM sh_incentive_i ...

  5. Keepalived+LVS(dr)高可用负载均衡集群的实现

    一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+lvs双主高可用负载均衡集群及LAMP应用keepalived-1.2 ...

  6. Android Binder机制中的异步回调

    “Binder通信是同步而不是异步的”,但是在实际使用时,是设计成客户端同步而服务端异步. 看看Framwork层的各service类java源码便会知道,在客户端调用服务端的各种方法时,通常会传递一 ...

  7. oracle删除重复数据

    select id from LOG where created >= to_date('2015/2/7 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and cre ...

  8. 界面主窗体,子窗体的InitializeComponent(构造函数)、Load事件执行顺序

    主窗体,子窗体的InitializeComponent(构造函数).Load事件执行顺序1.执行主窗体定义事件 new函数时,同时执行主窗体构造函数,默认就一个InitializeComponent函 ...

  9. Python基础学习三 字符串

    字符串方法 slit = ['a', 'b', 'c', 'd', 'f', 'g'] s2='hhhhhhhhhh' tu = (1,2,3,4,5) d={'name':'nnn','age':1 ...

  10. python的ftplib模块

    Python中的ftplib模块 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件 FTP的工作流程及基本操作可参考协议RFC95 ...