2013.07.05——— android LocalSocket
参考:http://www.cnblogs.com/bastard/archive/2012/10/09/2717052.html
http://www.cnblogs.com/over140/archive/2011/11/22/2258372.html

package com.example.testlocalsocket;import java.io.IOException;import java.io.InputStream;import android.app.Activity;import android.net.LocalServerSocket;import android.net.LocalSocket;import android.net.LocalSocketAddress;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { public static String SOCKET_ADDRESS = "123456Video";        private final Handler handler = new Handler();        public class NotificationRunnable implements Runnable {        private String message = null;                public void run() {            if (message != null && message.length() > 0) {                showNotification(message);            }        }                /**        * @param message the message to set        */        public void setMessage(String message) {            this.message = message;        }    }        // post this to the Handler when the background thread notifies    private final NotificationRunnable notificationRunnable = new NotificationRunnable();        public void showNotification(String message) {        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();    }        class SocketListener extends Thread {        private Handler handler = null;        private NotificationRunnable runnable = null;                public SocketListener(Handler handler, NotificationRunnable runnable) {            this.handler = handler;            this.runnable = runnable;            this.handler.post(this.runnable);        }                /**        * Show UI notification.        * @param message        */        private void showMessage(String message) {            this.runnable.setMessage(message);            this.handler.post(this.runnable);        }                @Override        public void run() {            //showMessage("DEMO: SocketListener started!");            try {                LocalServerSocket server = new LocalServerSocket(SOCKET_ADDRESS);                while (true) {                    LocalSocket receiver = server.accept();                    if (receiver != null) {                        InputStream input = receiver.getInputStream();                                                // simply for java.util.ArrayList                        int readed = input.read();                        int size = 0;                        int capacity = 0;                        byte[] bytes = new byte[capacity];                                                // reading                        while (readed != -1) {                            // java.util.ArrayList.Add(E e);                            capacity = (capacity * 3)/2 + 1;                            //bytes = Arrays.copyOf(bytes, capacity);                            byte[] copy = new byte[capacity];                            System.arraycopy(bytes, 0, copy, 0, bytes.length);                            bytes = copy;                            bytes[size++] = (byte)readed;                                                        // read next byte                            readed = input.read();                        }                                                showMessage(new String(bytes, 0, size));                    }                }            } catch (IOException e) {                Log.e(getClass().getName(), e.getMessage());            }        }    }        public static void writeSocket(String message) throws IOException {        LocalSocket sender = new LocalSocket();        sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));        sender.getOutputStream().write(message.getBytes());        sender.getOutputStream().close();    }        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                new SocketListener(this.handler, this.notificationRunnable).start();                Button send1 = (Button)findViewById(R.id.send);        send1.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                try {                    writeSocket("hello");                } catch (IOException e) {                    Log.e(getClass().getName(), e.getMessage());                }            }                   });    }}




更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android使用XML全攻略
  2. android上使用XML
  3. android强大的SpannableStringBuilder
  4. Android解析XML(PULL)展示到ListView
  5. android与C++的选择
  6. 安装Android的SDK--中文教程(完整版,包括Ec
  7. Android指纹解锁调用
  8. android 中Message、Handler、Message Qu
  9. Android用户版本分布更新 2.1版领先
  10. Android热补丁动态修复技术(完结篇):自动