dssfsfsfs
一直都好使啊。。好使 好使
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的更多相关文章
随机推荐
- thinkphp中的dump方法
感受一下,调试. 1.print_r() 2.var_dump() 3.再看看thinkphp中的dump方法 清晰多了!真实够傻的,今天才发现有这么好的调试方法.
- thinkphp中的多字段模糊匹配
引言:有时候查询要匹配多个字段.比如查询地址,地址是由多个字段组成的.有省.市.区等等,以及详细地址.这个时候如何查询呢? 实现不同字段相同的查询条件 $User = M("User&quo ...
- JavaScript笔记——基础知识(二)
Function类型 函数function不需要返回类型(不是没有返回值),参数也不需要指定类型,更为特殊的是函数竟然是个类,可以通过new出来 var box= new Function('num1 ...
- spring boot 集成 rabbitmq
1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...
- java mutilThread
可以参考https://www.cnblogs.com/wxd0108/p/5479442.html 这里补充一下我理解多线程这部分的一些要点. 1.首先是java的monitor概念:https:/ ...
- python学习笔记(三):文件操作和集合
对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...
- pandas分组和聚合
Pandas分组与聚合 分组 (groupby) 对数据集进行分组,然后对每组进行统计分析 SQL能够对数据进行过滤,分组聚合 pandas能利用groupby进行更加复杂的分组运算 分组运算过程:s ...
- 1.2celery的安装
我们可以使用python的包管理器pip来安装: pip install -U Celery 也可从官方直接下载安装包:https://pypi.python.org/pypi/celery/ tar ...
- 字节流之文件输入流FileInputStream
- JAVA基础知识总结18(反射)
反射技术: 其实就是动态加载一个指定的类,并获取该类中的所有的内容.而且将字节码文件封装成对象,并将字节码文件中的内容都封装成对象,这样便于操作这些成员.简单说:反射技术可以对一个类进行解剖. 反射的 ...