Android--推送机制实现原理(三)-自己实现推送功能--建立长连接

来源:互联网 发布:西宁数据工程师招聘 编辑:程序博客网 时间:2024/06/01 13:16

Android Socket编程参考博客:http://blog.csdn.net/x605940745/article/details/17001641
Java 多线程单例模式编程参考博客:http://blog.csdn.net/jason0539/article/details/23297037/


 使用多线程单例模式,建立长连接之后所有的线程就只生成一个实例,然后对这个实例进行接收数据线程、发送数据线程和心跳包线程。然后慢慢的进行线程调控。这里涉及Java的多线程编程。

Socket socket = null;String buffer = "";String geted1;public Handler myHandler = new Handler() {    @Override    public void handleMessage(Message msg) {        if (msg.what == 0x11) {            Bundle bundle = msg.getData();            txt_Show.append("server:"+bundle.getString("msg")+"\n");        }    }};rootView4 = inflater.inflate(R.layout.fragment_four,container,false);editText = (EditText) rootView4.findViewById(R.id.input);btn_Send = (Button) rootView4.findViewById(R.id.send);txt_Show = (TextView) rootView4.findViewById(R.id.show); btn_Send.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            geted1 = editText.getText().toString();            txt_Show.append("client:"+geted1+"\n");//                txt_Show.append("yuanlief\n");//                启动线程 向服务器发送和接收信息            new MyThread(geted1).start();        }    });    class MyThread extends Thread {        public String txt;        public MyThread(String str) {            txt = str;        }        @Override        public void run() {            //定义消息            Message msg = new Message();            msg.what = 0x11;            Bundle bundle = new Bundle();            bundle.clear();            try {                //连接服务器 并设置连接超时为5秒                socket = new Socket();                try {                    socket.connect(new InetSocketAddress("101.200.46.138", 30000), 5000);                } catch (IOException e) {                    e.printStackTrace();                }                //获取输入输出流                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("android 客户端".getBytes("gbk"));                ou.flush();                bundle.putString("msg", buffer.toString());                msg.setData(bundle);                //发送消息 修改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();            }        }    }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <EditText        android:id="@+id/input"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/input" />    <Button        android:id="@+id/send"        android:text="@string/send"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/input"        android:layout_alignParentStart="true"        android:layout_marginTop="13dp" />    <TextView        android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/send"        android:layout_alignParentStart="true"        android:layout_marginTop="31dp" /></RelativeLayout>
0 0