import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import com.example.hfut.ClientService.ClientCallback;
public class MainActivity extends Activity implements ClientCallback {
private ImageButton w = null;
private ClientService client_service = new ClientService();
private ClientService.MsgBinder binder = null;
private boolean is_bind = false;
private Handler handler = null;
private CSConnection csconn = null;
private String ip = null;
private String port = null;
private int i = 0;
private final int START_ANIM = 0x1;
@SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
initViews();
Intent intent = getIntent();
ip = intent.getStringExtra("ip");
port = intent.getStringExtra("port");
i = Integer.parseInt(port);
// System.out.println("binder1----------->" + binder);
new Thread() {
public void run() {
client_service.initSocket(ip, i);
startCustomService();
System.out.println("binder2----------->" + binder);
}
}.start();
startCustomService();
w.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// startCustomService();
System.out.println("binder3----------->" + binder);
binder.sendText("w"); //此处不能发送
System.out.println("w----------->" + w);
}
});
}
private void initViews() {
w = (ImageButton) findViewById(R.id.w);
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
is_bind = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (ClientService.MsgBinder) service;
client_service = (ClientService) binder.getSevice();
client_service.setCallback(MainActivity.this);
is_bind = true;
}
};
private void startCustomService() {
Intent intent = new Intent(this, ClientService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
private void stopCutomService() {
if (is_bind) {
unbindService(conn);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopCutomService();
}
@Override
public void runAnimation() {
handler.sendEmptyMessage(START_ANIM);
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Service;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class ClientService extends Service {
private static Socket socket = null;
private static PrintWriter out = null;
private ClientCallback client_callback = null;
private Drawable drawable = null;
private boolean is_need_stop = false;
@Override
public IBinder onBind(Intent intent) {
IBinder result = null;
if (null == result)
result = new MsgBinder();
Toast.makeText(this, "onBind", Toast.LENGTH_LONG).show();
return result;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("onCreate.....");
Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
is_need_stop = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public void setCallback(ClientCallback clientcallback) {
this.client_callback = clientcallback;
}
public void initSocket(String s, int i) {
try {
if (socket != null) {
socket.close();
socket = null;
}
socket = new Socket(s, i);
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("socket------->" + socket);
System.out.println("out------->" + out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
// private void startAnim() {
// if (client_callback == null) {
// return;
// }
// client_callback.runAnimation();
// }
// private void releaseLastSocket(Socket mSocket) {
// try {
// if (null != mSocket) {
// if (!mSocket.isClosed()) {
// mSocket.close();
// }
// mSocket = null;
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
public class MsgBinder extends Binder {
public Service getSevice() {
return ClientService.this;
}
public void sendText(String text) {
if (socket == null) {
System.out.println("socket-null------->" + socket);
System.out.println("out-null-------->" + out);
return;
}
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
System.out.println("socket-notnull------->" + socket);
out.print(text);
System.out.println("out-notnull-------->" + out);
out.flush();
}
}
}
public boolean sendMsg(String msg) {
if (null == socket) {
return false;
}
try {
if (!socket.isClosed() && !socket.isOutputShutdown()) {
OutputStream os = socket.getOutputStream();
String message = msg;
os.write(message.getBytes());
os.flush();
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public void receive() {
stop();
}
public void stop() {
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
out.println("exit");
out.flush();
}
}
}
}
interface ClientCallback {
public void runAnimation();
}
}
#Service已经在Mainfest里面绑定了
new Thread() {
public void run() {
client_service.initSocket(ip, i);//这里的client_service是你自己new出来的,private ClientService client_service = new ClientService(); 不是ServiceConnection返回的。这时候你的service还没启动呢,怎么可能能设置呢。所以这段代码有问题,先启动service,在初始化socket 也就是把这段线程启动放到onServiceConnected里面去初始化socket。
startCustomService();
System.out.println("binder2----------->" + binder);
}
}.start();
startCustomService();