Im 건물 소켓을 통해 서버에 연결하는 안드로이드 응용 프로그램을 구축. 그러나 메인 스레드를 잠글 것이고 android가 그것을 허용하지 않기 때문에 나는 worker 연결 스레드로부터 mainthread를 통지 할 수 없습니다. 여기에 다음 코드 나는이있다 : 연결 컨트롤러의안드로이드 소켓 communcation
부 :
public void run(){
while (true){
while (isRunning){
if (serverSocket == null){
try{
serverSocket = new Socket("*", *);
out = new PrintWriter(serverSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
serverSocket.getInputStream()));
}
catch (IOException e){
e.printStackTrace();
}
}
if (message != ""){
try{
System.out.println(message);
out.println(message);
message = "";
reply = in.readLine();
isRunning = false;
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void sendMessage(String path,
HashMap<String, HashMap<String, String>> variables){
this.reply = "";
isRunning = true;
String variablesJSON = JSONValue.toJSONString(variables);
this.message = path + ":" + variablesJSON;
}
인증 클래스 :
public boolean register(String email, String password, String displayName) {
String path = "User.register";
HashMap<String, String> variables = new HashMap<String, String>();
HashMap<String, HashMap<String, String>> user = new HashMap<String, HashMap<String, String>>();
String hashedPass;
try{
hashedPass = sha1(password);
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
return false;
}
variables.put("displayname", displayName);
variables.put("password", hashedPass);
variables.put("email", email);
user.put("User", variables);
connection.sendMessage(path, user, this);
final ProgressDialog progDailog = ProgressDialog.show(context,
"Please wait..", "Register", true);
new Thread(){
public void run(){
try{
while (ConnectionController.getInstance().getIsRunning()){
sleep(200);
}
progDailog.dismiss();
}
catch (Exception e){
e.printStackTrace();
}
}
}.start();
Toast.makeText(context.getApplicationContext(),
ConnectionController.getInstance().getReply(), Toast.LENGTH_LONG)
.show();
if (ConnectionController.getInstance().getReply() != "SUCCESS"){
return false;
}
return true;
}
을 나는 기다릴 필요 ProgressDialog를 끝내지 만, 길을 찾을 수 없습니다. AsyncTask가 연결을 항상 열려야하므로이 작업을 수행하는 올바른 방법이라고 생각하지 않습니다. 어떤 힌트 또는 아이디어?
[이 문서는 (https://developer.android.com/training/multiple-threads/communicate-ui.html)을 읽으십시오. 스레드 및 핸들러 인스턴스 내에서 runnable을 사용하여 UI 스레드의 메시지를 처리 할 수 있습니다. – grine4ka