我正在编写套接字客户端应用程序。
App非常示例,Activty启动一个新的套接字线程来连接服务器。 thread recive命令来自服务器并通过setImageDrawable更改ImageView。
布局中只有一个ImageView。 socket线程将使用BufferedReader从服务器获取命令,它的命令行是" start&#34 ;,套接字线程将向主活动发送消息以执行setImageDrawable。
这是我的问题: PORTRAIT模式下一切都很好。但是在LANDSCAPE模式下运行了。
打击布局:
package com.example.whiletest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class WhileTest extends Activity {
private ImageView ivTest;
private TextView tvState;
private Handler handler = null;
private String strTemp;
private Message message=new Message();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* set app to LANDSCAPE mode */
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_while_test);
tvState = (TextView) findViewById(R.id.textView1);
ivTest = (ImageView) findViewById(R.id.imageView1);
/* Change UI by main activity*/
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 110:
ivTest.setImageDrawable(getResources().getDrawable(R.drawable.btn_camera_recording));
ivTest.postInvalidate();
System.out.println("OKAYAYHHAHS");
break;
case 112:
tvState.setText("收到指令 = " + strTemp);
break;
}
super.handleMessage(msg);
}
};
new Thread(){
@Override
public void run()
{
Socket socketNet;
try{
socketNet = new Socket("192.168.2.104",3302); //make connection
System.out.println("Connected!!!");
BufferedReader bff=new BufferedReader(new InputStreamReader(socketNet.getInputStream()));
PrintWriter os=new PrintWriter(socketNet.getOutputStream());
while ((strTemp = bff.readLine()) != null){
//got command from server then send message to main activity
System.out.println("readLine = " + strTemp);
message.what=112;
handler.sendMessage(message);
if( strTemp.equals("start")){
message.what=110;
handler.sendMessage(message);
}
};
} catch (SocketTimeoutException aa) {
System.out.println("TimeOut");
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
}
}
下面是布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.whiletest.WhileTest" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="138dp"
android:layout_marginTop="163dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
这是server.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
public class Server{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(3302);
}catch(Exception e) {
System.out.println("can not listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept();
System.out.println("accepted.");
}catch(Exception e) {
System.out.println("Error."+e);
}
String line="start";
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
final PrintWriter os=new PrintWriter(socket.getOutputStream());
Timer timer=new Timer();
timer.schedule(new TimerTask(){
public void run() {
os.println("start");
os.flush();
//System.out.println("Command Sended" + String.valueOf(System.currentTimeMillis()));
}
} , 1000, 1000);
}catch(Exception e){
System.out.println("Error:"+e);
}
}
}
我已经为它做了3天。请帮帮我。
答案 0 :(得分:0)
尝试在清单文件中,在您所在的活动下添加此行:
android:configChanges="orientation|screenSize"