安卓写的比较少,主要写一下嵌入式相关的东西,例如wifi,蓝牙。
这里写一个socket客服端的例子,有读写,有在线离线检测,新手写的少,勿喷,,
笨程序只用到2个编辑框,和几个按钮,布局文件放在最后。
记得在添加权限(网络权限)
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 上个丑图: 程序代码: package com.example.client; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.EOFException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; public class MainActivity extends AppCompatActivity { Socket cfd; //客服端套接字 OutputStream os; BufferedWriter bw; //写buf PrintWriter pw; BufferedReader br; //读buf EditText send_edit; EditText recv_edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); data_init(); } /*数据初始化 * * * */ public void data_init() { new Thread(new Runnable() { @Override public void run() { send_edit= (EditText) findViewById(R.id.send_msg); //发送数据的编辑控件 recv_edit= (EditText) findViewById(R.id.recv_msg); //接收数据控件 } }).start(); } /*连接服务器代码 * * * */ public void startClient(View view) { new Thread(new Runnable() { @Override public void run() { try{ if(cfd.isBound()) { //判断是否已近连接服务器 Message msg = new Message(); msg.what = 5; mHandler.sendMessage(msg); } }catch (Exception e) //没有连接就连接服务器 { try { cfd= new Socket("47.93.186.13",88); if(cfd.isBound()) //连接成功 { try { os=cfd.getOutputStream(); bw=new BufferedWriter(new OutputStreamWriter(os)); pw=new PrintWriter(bw,true); recv(); } catch (IOException e2) { e.printStackTrace(); } Message msg=new Message(); msg.what=1; mHandler.sendMessage(msg); } } catch (IOException e1) { //连接失败 Message msg=new Message(); msg.what=2; mHandler.sendMessage(msg); e.printStackTrace(); } } } }).start(); } /*断开服务器代码 * * * */ public void closeClient(View view) { new Thread(new Runnable() { @Override public void run() { try { if(cfd.isBound()) //服务器是否连接 { try { cfd.close(); cfd=null; Message msg=new Message(); msg.what=3; mHandler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } }catch (Exception e) //服务器未连接 { Message msg=new Message(); msg.what=4; mHandler.sendMessage(msg); } } }).start(); } /*发送代码 * * * */ public void send(View view) { new Thread(new Runnable() { @Override public void run() { try { if(cfd.isBound()) //是否接服务器 { pw.println(send_edit.getText().toString()); } }catch (Exception e) //未连接服务器不能发送 { Message msg=new Message(); msg.what=6; mHandler.sendMessage(msg); } } }).start(); } /*接收代码 * * * */ public void recv() { new Thread(new Runnable() { @Override public void run() { try{ br=new BufferedReader(new InputStreamReader(cfd.getInputStream())); }catch (IOException e) { } while(true) { String res = null; try { res=br.readLine(); //读取一行 } catch (IOException e) { e.printStackTrace(); } if(res!=null) { Message msg=new Message(); //把消息给handler处理 msg.what=9; //消息的id msg.obj=res; //把string数据给obj对象 mHandler.sendMessage(msg); } } } }).start(); } //处理消息改变界面 Handler mHandler= new Handler(new Handler.Callback(){ @Override public boolean handleMessage(Message msg) { if(msg.what==1) { Toast toast_true = Toast.makeText(MainActivity.this,"连接服务器成功",Toast.LENGTH_SHORT); toast_true.show(); } if(msg.what==2) { Toast toast_flase = Toast.makeText(MainActivity.this,"连接服务器失败",Toast.LENGTH_SHORT); toast_flase.show(); } if(msg.what==3) { Toast toast_flase = Toast.makeText(MainActivity.this,"断开服务器",Toast.LENGTH_SHORT); toast_flase.show(); } if(msg.what==4||msg.what==6) { Toast toast_flase = Toast.makeText(MainActivity.this,"未连接服务器",Toast.LENGTH_SHORT); toast_flase.show(); } if(msg.what==5) { Toast toast_flase = Toast.makeText(MainActivity.this,"服务器已近连接",Toast.LENGTH_SHORT); toast_flase.show(); } if(msg.what==9) //显示消息 { recv_edit.append(msg.obj.toString()+"\n"); } return false; } }); } 这里稍微解释一下每个函数都使用线程的原因,因为主线程是app界面的,如果用来运行一些带堵塞或者其它不可描述的函数会导致主线程那啥(造成闪退)具体原因也不深究,毕竟我也不是搞安卓的。主线程不能直接去改变界面(UI)上的数据,所以这里采用消息机智交给handler处理,上面有注释应该都看得懂
代码很少
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.client.MainActivity" android:background="#f0ff0f" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:text="HES_C" android:textSize="40dp" android:gravity="center" android:background="#0fd0ff" /> <LinearLayout android:background="#003344" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="连接服务器" android:layout_weight="1" android:onClick="startClient" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="断开服务器" android:layout_weight="1" android:onClick="closeClient" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:background="#112233" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" > <EditText android:id="@+id/send_msg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff0e0" /> <Button android:layout_weight="2" android:text="发送" android:layout_width="match_parent" android:layout_height="match_parent" android:onClick="send"/> </LinearLayout> <LinearLayout android:layout_weight="7" android:background="#ffffe0" android:layout_width="match_parent" android:layout_height="0dp"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/recv_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:enabled="false" /> </ScrollView> </LinearLayout> </LinearLayout>