Wednesday, February 5, 2014

Activity started a thread loop unresponsive


 
package com.example.myproject;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowTerminal extends Activity implements Runnable {

private Socket socket;
private String IP = "127.0.0.1";
private FileInputStream in = null;
private InputStreamReader reader = null;
private String name = null;
private int CurrentCode = 0;
private InputStream ins;
private OutputStream outs;
private List Elist = null;
private int stateCode;
private TextView tv;
private Button btn;
private Thread thread;
private Thread recvthread;
@ Override
protected void onCreate (Bundle savedInstanceState) {
/ / TODO Auto-generated method stub
super.onCreate (savedInstanceState);
setContentView (R.layout.showterminal);
tv = (TextView) findViewById (R.id.ename);
btn = (Button) findViewById (R.id.onoff);
Intent intent = getIntent ();
Bundle bundle = intent.getExtras ();
name = bundle.getString ("name");
tv.setText (name);
btn.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
int [] comBuf = new int [2];
/ / TODO Auto-generated method stub
if (stateCode == 0) {
comBuf [0] = CurrentCode;
comBuf [1] = 1;
}
else {
comBuf [0] = CurrentCode;
comBuf [1] = 0;
}
byte [] cmd = intToByteArray (comBuf, 2);
try {
outs.write (cmd);
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
});
thread = new Thread (ShowTerminal.this);
thread.start ();

}
public class EnameCode {
public String Ename = null;
public int Ecode = 0;
}
public static byte [] intToByteArray (int [] value, int n) {
byte [] b = new byte [n * 4];
for (int j = 0; j for (int i = j * 4; i <4 * n; i + +) {
int offset = (b.length - 1 - i) * 8;
b [i] = (byte) ((value [j] >>> offset) & 0xFF);
}
return b;
}
public static int bytesToInt (byte [] data, int offset) {
int num = 0;
for (int i = offset; i num << = 8;
num | = (data [i] & 0xff);
}
return num;
}
public void initCode () {
Elist = new ArrayList ();
int sig = 1;
int m = 1;
try {
in = openFileInput ("smarthouse.txt");
reader = new InputStreamReader (in, "UTF16");
BufferedReader br = new BufferedReader (reader);
String s = null;
EnameCode ec = new EnameCode ();
while ((s = br.readLine ())! = null) {
m = sig% 2;
sig + +;
if (m! = 0) {
ec.Ename = s;
}
else {
ec.Ecode = Integer.parseInt (s);
Elist.add (ec);
}
}
} Catch (FileNotFoundException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (IOException E) {
E.printStackTrace ();
}
for (EnameCode e: Elist) {
if (e.Ename.equals (name)) {
CurrentCode = e.Ecode;
}
}
}
Handler mHandler = new Handler () {
public void handleMessage (Message msg) {
if (msg.arg1 == -1 | | msg.arg1 == -2) {
tv.setText ("error");
}
if (msg.arg1 == 0) {
tv.setText ("open");
}
if (msg.arg1 == 1) {
tv.setText ("Off");
}

}
};
@ Override
public void run () {
/ / TODO Auto-generated method stub
int nameCode;
Message m = mHandler.obtainMessage ();
socket = new Socket ();
try {
socket.connect (new InetSocketAddress (IP, 8888));
ins = new DataInputStream (socket.getInputStream ());
outs = new DataOutputStream (socket.getOutputStream ());
int [] command = {CurrentCode, 1};
outs.write (intToByteArray (command, 2));
while (! thread.currentThread (). isInterrupted ()) {

byte [] buffer = new byte [ins.available ()];
ins.read (buffer);
if (buffer.length == 8) {
nameCode = bytesToInt (buffer, 0);
stateCode = bytesToInt (buffer, 1);
if (nameCode! = CurrentCode) {
m.arg1 = -2;
}
else {
m.arg1 = stateCode;
}
}
else {
m.arg1 = -1;
}
mHandler.sendMessage (m);
}
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}


@ Override
protected void onDestroy () {
/ / TODO Auto-generated method stub
if (thread! = null) {
thread.interrupt ();
thread = null;
}
super.onDestroy ();
/ / SetContentView (null);
}

}

Why does it not start this Activity, and if you comment out the thread loop can be started, but quit after being given the open thread has begun
Reply:
Your idea is too wonderful an activity inherited Runnable
I said this ...
Reply:
cited a floor tanwei4199 reply:
you think the activity is too wonderful inheritance Runnable
I said this ...
It's not critical, I wanted to know why does not respond
Reply:
reference to the second floor bdyz1016 reply:
Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct another thread from the right
Reply:
reference to the third floor tanwei4199 reply:
Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct from another thread it

Reply:
references, 4th Floor xp460757952 reply:
Quote: references to the third floor tanwei4199 reply:

Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct from another thread it
rely on, you will not Bite your tongue, do you want me to give you a code that can run
Reply:
reference to the third floor tanwei4199 reply:
Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct from another thread it
My run is the main thread? School learned ah
Reply:
reference to the 6th floor bdyz1016 reply:
Quote: references to the third floor tanwei4199 reply:

Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct from another thread it
My run is the main thread? Learn learned ah


I think your main thread to the thread loop obstruction dead right, you try to add a sleep?
Reply:
reference to the 7th floor baiyewu19 reply:
Quote: references to the 6th floor bdyz1016 reply:

Quote: references to the third floor tanwei4199 reply:

Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tanwei4199 reply:

Your idea is too wonderful an activity inherited Runnable
I said this ...
It's not critical, I wanted to know why do not respond

I regret to tell you that is the key
activity in the main thread to run your networking die strange conduct from another thread it
My run is the main thread? Learn learned ah


I think your main thread to the thread loop obstruction dead right, you try to add a sleep?
may be right, I do not know what's changed for the better

No comments:

Post a Comment