編輯:關於 Android 程式設計
本文示範如何讓 Android 手機作為用戶端,透過 Socket 連上電腦上的 Java 伺服器,雙方互相傳送訊息。這是最基礎的 TCP 通訊範例,很適合作為理解網路連線的起點。
伺服器先在 1000 埠監聽,接受連線後讀取一行訊息,加上前綴再回傳給用戶端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(1000);
Socket clink = server.accept();
PrintStream out = new PrintStream(clink.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clink.getInputStream()));
StringBuffer bu = new StringBuffer();
bu.append("Android: ");
bu.append(in.readLine());
out.println(bu);
in.close();
out.close();
clink.close();
server.close();
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="連接ServerSocket程序" />
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="等待程序的連接" />
</LinearLayout>
public class ActivityMain extends Activity {
private Button button;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.send);
textView = (TextView) this.findViewById(R.id.info);
button.setOnClickListener(new SendOnclickListener());
}
class SendOnclickListener implements OnClickListener {
public void onClick(View v) {
try {
Socket clink = new Socket("10.10.104.57", 1000);
PrintStream out = new PrintStream(clink.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clink.getInputStream()));
out.println("發送信息給ServerSocket");
ActivityMain.this.textView.setText(in.readLine());
in.close();
out.close();
clink.close();
} catch (Exception e) {
}
}
}
}
別忘了在 AndroidManifest.xml 註冊 INTERNET 權限,否則程式會無法連線:
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.android.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityMain"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
完成以上三步,點擊按鈕即可讓手機把訊息送到電腦,並把伺服器的回覆顯示在 TextView 上。
Android 開發技巧:用 PopupWindow 封裝彈出式選單
由於介面的客製需求,Android 原生的彈出選單往往不敷使用,自定義選單幾乎是唯一選擇。本文分享一個把 PopupWindow 封裝成彈出選單的類別 PopMenu,以
ListView 進階用法總整理:自訂版面、效能優化與單位換算
ListView 在 Android 開發中的使用頻率極高,值得花時間把它的完整用法與效能要點整理清楚。本文以 ListViewTest 專案為例,從最簡單的字串列表出發
Android 底層實例:耳機插拔檢測與喇叭/聽筒通道切換
Android 底層開發之耳機插拔與音頻通道切換實例 這台設備用的是耳機與麥克分離式的設計,因此需要分別上報事件。排查過程大致是:先確認插拔檢測正常,再確認 ALSA 通
自實作標籤容器控制項:模仿 ListView 的 Adapter 設計
要做的東西 很多 app 裡都有那種「標籤雲」——一串膠囊狀的文字標籤,會自動換行、自動適應螢幕寬度。它看起來像 LinearLayout,但 LinearLayout