Android LocalSocket / LocalServerSocket sample code
来源:互联网 发布:洞主的手工皂淘宝店 编辑:程序博客网 时间:2024/05/16 15:04
Add one button into your layout (main.xml):
<Button android:id="@+id/send_1_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="send 1 request"/>
And this simple code for your activity
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;/**** @author Denis Migol**/public class DemoActivity extends Activity { public static String SOCKET_ADDRESS = "your.local.socket.address"; // background threads use this Handler to post messages to // the main application thread 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.main); new SocketListener(this.handler, this.notificationRunnable).start(); Button send1 = (Button)findViewById(R.id.send_1_button); send1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { writeSocket("hello"); } catch (IOException e) { Log.e(getClass().getName(), e.getMessage()); } } }); }}
- Android LocalSocket / LocalServerSocket sample code
- LocalSocket / LocalServerSocket
- LocalSocket/LocalServerSocket
- 整理下android LocalServerSocket LocalSocket的用法
- Android之LocalSocket和LocalServerSocket的使用
- LocalSocketAddress LocalServerSocket LocalSocket 用法?
- android 跨进程通信 LocalServerSocket LocalSocket Address already in use
- 网路编程-UDP-DatagramSocket、DatagramPacket ||LocalServerSocket和LocalSocket
- 虹软人脸识别Android Sample Code
- android --- LocalSocket
- android localSocket
- Android localSocket
- Android LocalSocket
- Android LocalSocket
- sample code
- android获取wifi信息sample code
- 【转】Android LockScreen admin API sample code
- Android Input Method Sample Code Analysis
- sql server 2005中如何查询一个数据库中有多少张表?
- canvas 简单实用
- php移位运算
- Hibernate读书笔记1-对象持久化模式
- 基于网络环境的程序设计综合实验-导学
- Android LocalSocket / LocalServerSocket sample code
- Web Service
- Eclipse没有编译class文件
- JVM启动参数大全
- 模态窗口和非模态窗口
- Android笔记(1)——不同apk之间传递参数与数据
- Facebook和Twitter之后,我们用什么?
- SSH的一个BaseDao的整合
- 关于ModifyStyleEx无效的问题