刚刚接触android,想建立android与PC之间的TCP连接,android端按下拍照按键后向PC端发送一个自定义信息,但是程序安装到设备后根本不能连接到PC,请帮忙找找原因!
MacService.java:
package com.gzmacweb.macclient;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class MacService extends Service {
private static final String TAG = "MacService";
PrintWriter out;
@Override
public void onCreate() {
// TODO
try{
InetAddress serverAddr = InetAddress.getByName("192.168.10.168");//TCP服务器IP地址
Log.d("TCP", "服务器:正在连接...");
Socket socket = new Socket(serverAddr,747);
String message = "你好!Android。";
try {
Log.d("TCP", "服务器正在发送信息:'"+message+"'");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
} catch (Exception e) {
Log.e("TCP", "服务器出错",e);
}finally{
socket.close();
}
}catch(Exception e){
Log.e("TCP", "服务器出错",e);
}
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle=intent.getExtras();
if(bundle!=null&&bundle.containsKey("send")){//
out.println("kaca");
out.flush();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
广播接收器MacBroadcastReceiver.java:
package com.gzmacweb.macclient;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MacBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION_CAPTURE = "com.hikvision.keyevent.CAPTURE";
private static final String ACTION_BOOT = Intent.ACTION_BOOT_COMPLETED;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null)
return;
if (action.equals(ACTION_CAPTURE)) {
Intent service = new Intent(context, MacService.class);
service.putExtra("send", true);
context.startService(service);
} else if (action.equals(ACTION_BOOT)) {
Intent service = new Intent(context, MacService.class);
context.startService(service);
}
}
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gzmacweb.macclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher" >
<!-- 注册自定义静态广播接收器 -->
<receiver android:name=".MacBroadcastReceiver">
<intent-filter>
<action android:name="com.hikvision.keyevent.CAPTURE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MacService" >
</service>
</application>
</manifest>
就把TCP那部分代码在android上当做java来处理就可以了。 。那个finally我没看明白什么意思, 发一条消息socket就关掉?
/* 连接服务器 */
socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);
/* 获取输出流 */
output = new PrintStream(socket.getOutputStream(), true, "utf-8");
output.print(msg);
还有在AndroidManifest.xml中添加访问网络权限:
<uses-permission android:name="android.permission.INTERNET" />
还有一点,你是在oncreate()中实现socket的,而oncreate()不是每次都调用,最好在onStartCommand()中实现。