http://www.javashuo.com/article/p-fnmkooai-bu.htmlhtml
有些很細緻的東西參考這篇 http://www.javashuo.com/article/p-ntvnwbez-s.html 看看就能夠android
咱用Android Studio作服務器
包名 路徑什麼的本身隨意哈網絡
放一個textview 本身拖動,哈socket
因爲沒有Edittext 控件,因此,沒有能夠拖進來的.....ide
選擇自適應測試
若是沒有提示 能夠 CTRL + ALT +空格spa
忘了一件事情,設置下控件的ID 實際上系統會自動設置,可是最好仍是本身設置,避免之後文件多了ID衝突3d
我通常命名規則是 這個控件的名稱+該控件所在xml文件的文件名而後後面是1,2,3,4..................有多少個相同的控件就這樣排code
public class MainActivity extends AppCompatActivity { EditText editTextActivityMain1,editTextActivityMain2;//定義兩個EditText變量,變量名字我通常設置的和先前頁面裏面的同樣 Button buttonActivityMain1;//鏈接按鈕 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控件 editTextActivityMain1 = findViewById(R.id.editTextActivityMain1); editTextActivityMain2 = findViewById(R.id.editTextActivityMain2); buttonActivityMain1 = findViewById(R.id.buttonActivityMain1); } }
鏈接網絡通訊須要加權限,我通常都會一次性把本身全部可能用到的都加進去
<uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <!--讀手機內容的權限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
其實只須要,,我是爲了之後作軟件省時省事
因爲鏈接的時候是阻塞的(鏈接不上就一直等着,直到鏈接錯誤,或者超時),因此須要任務
仍是用的
http://www.javashuo.com/article/p-gbbgtcpm-e.html 這節的程序
接着完善
因爲不能在任務裏面操做控件,因此咱須要用 Handler
和當時 http://www.javashuo.com/article/p-abwyufaz-e.html
同樣的道理,只不過解決的方法不同
public class MainActivity extends AppCompatActivity { EditText editTextActivityMain1,editTextActivityMain2;//定義兩個EditText變量,變量名字我通常設置的和先前頁面裏面的同樣 Button buttonActivityMain1;//鏈接按鈕 Socket socket;//定義一個Socket MyHandler myHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myHandler = new MyHandler(); //獲取控件 editTextActivityMain1 = findViewById(R.id.editTextActivityMain1); editTextActivityMain2 = findViewById(R.id.editTextActivityMain2); buttonActivityMain1 = findViewById(R.id.buttonActivityMain1); //鏈接按鈕點擊事件 buttonActivityMain1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (buttonActivityMain1.getText().toString() == "鏈接"){ new Thread(new Runnable() { @Override public void run() { //這裏面寫具體的程序 try { socket = new Socket("192.168.4.1",8888);//鏈接TCP服務器 if (socket.isConnected()){//若是鏈接上TCP服務器 Log.e("MainActivity", "isConnected"); Message msg = myHandler.obtainMessage();//從消息隊列拉取個消息變量 msg.what = 1;//設置消息變量的 what 變量值 爲1 myHandler.sendMessage(msg);//插入消息隊列 } } catch (Exception e){ Log.e("MainActivity", e.toString()); } } }).start(); } else { try{ socket.close(); }catch (Exception e){} //關閉鏈接 buttonActivityMain1.setText("鏈接");//按鈕顯示鏈接 } } }); } //Handler class MyHandler extends Handler { public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1){//接收到消息變量的 what 變量值 爲1 buttonActivityMain1.setText("斷開");//按鈕顯示斷開 } } } }
測試
如今用上上面的IP和端口號
socket = new Socket(editTextActivityMain1.getText().toString(),Integer.valueOf(editTextActivityMain2.getText().toString()))
public class MainActivity extends AppCompatActivity { EditText editTextActivityMain1,editTextActivityMain2;//定義兩個EditText變量,變量名字我通常設置的和先前頁面裏面的同樣 Button buttonActivityMain1;//鏈接按鈕 Socket socket;//定義一個Socket MyHandler myHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myHandler = new MyHandler(); //獲取控件 editTextActivityMain1 = findViewById(R.id.editTextActivityMain1); editTextActivityMain2 = findViewById(R.id.editTextActivityMain2); buttonActivityMain1 = findViewById(R.id.buttonActivityMain1); buttonActivityMain1.setText("鏈接");//加上這個,不然按鈕須要點擊兩次纔會判斷按鈕的Text是"鏈接" //鏈接按鈕點擊事件 buttonActivityMain1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (buttonActivityMain1.getText().toString() == "鏈接"){ new Thread(new Runnable() { @Override public void run() { //這裏面寫具體的程序 try { socket = new Socket(editTextActivityMain1.getText().toString(),Integer.valueOf(editTextActivityMain2.getText().toString()));//鏈接TCP服務器 if (socket.isConnected()){//若是鏈接上TCP服務器 Log.e("MainActivity", "isConnected"); Message msg = myHandler.obtainMessage();//從消息隊列拉取個消息變量 msg.what = 1;//設置消息變量的 what 變量值 爲1 myHandler.sendMessage(msg);//插入消息隊列 } } catch (Exception e){ Log.e("MainActivity", e.toString()); } } }).start(); } else { try{ socket.close(); }catch (Exception e){} //關閉鏈接 buttonActivityMain1.setText("鏈接");//按鈕顯示鏈接 } } }); } //Handler class MyHandler extends Handler { public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1){//接收到消息變量的 what 變量值 爲1 buttonActivityMain1.setText("斷開");//按鈕顯示斷開 } } } }