마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

핸들러메세지를 이용한 화면전환

0 추천
제가 TCP/IP이용한 어플 제작중인데, 쓰레드 때문에 핸들러를 이용해서 화면전환을 하려고 Intent를 사용하였습니다.

Intent intentRegisterActivity = new Intent(AndroidSocketClient.this, RegisterActivity.class);
                startActivity(intentRegisterActivity);

이런 식으루요. 이걸 다른 어플에서 확인했을 때는 정상적으로 전환이 됬는데 지금 제작하고 있는 어플에서는 핸들러 안case문까지 들어왔는데 뻗어버리네요.......................이유를 알고 싶습니다 알려주세요ㅠㅠㅠㅠㅠ

 

 

package com.example.androidsocketclientextendsactivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidSocketClient extends Activity {

    private Socket socket;
    BufferedReader socket_in;
    PrintWriter socket_out;
    EditText input1, input2;
    Button button, button1;
    TextView output;
    String data;
    String obj;
    Handler mhandler1;
    Runnable mRunnable;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //mhandler1 = new Handler();
        input1 = (EditText) findViewById(R.id.input1);////////// id입력
        input2 = (EditText) findViewById(R.id.input2);////////// 비번입력
        button = (Button) findViewById(R.id.button);//////////// 로그인 버튼
        output = (TextView) findViewById(R.id.output);////////// 로그인 됬는지 뜨는거
        button1 = (Button) findViewById(R.id.Button01);

        /*mRunnable = new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(getApplicationContext(), Reservation.class);
                startActivity(intent);
            }
        };*/

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mhandler1 = new Handler();
                socket_out.println("1");//////////////// 1을 전송(서버에서 로그인모드)
                String data = (input1.getText().toString()) + "/" + (input2.getText().toString());
                Log.w("NETWORK", " " + data);
                if (data != null) {
                    socket_out.println(data);///////////////// data전송 (아이디와
                                                ///////////////// 비밀번호)
                }

            }
        });

        button1.setOnClickListener(new OnClickListener() {////////////// 회원가입페이지로
                                                            ////////////// 넘어감.
            public void onClick(View v) {
                
                socket_out.println("2");//////////////// 2를 전송(서버에서 회원가입모드)
            }
        });

        Thread worker = new Thread() {
            public void run() {
                try {
                    //socket = new Socket("165.229.229.176", 8080);
                    //socket=new Socket("221.157.0.141",64120);
                    socket=new Socket("192.168.0.14",7000);
                    socket_out = new PrintWriter(socket.getOutputStream(), true);
                    socket_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    while (true) {
                        ObjectInputStream instream = new ObjectInputStream(socket.getInputStream());
                         obj = (String) instream.readObject();

                        if (obj.equals("2")) {////////////// 회원가입 버튼 누른거라서
                                                ////////////// 회원가입페이지로 이동.
                            setting(obj);
                            //mHandler.sendEmptyMessage(2);
                        } else {
                            Log.e("1111", "" + obj);
                            //
                            Log.e("2222", "" + obj);
                            // mHandler.sendEmptyMessage(1);
                            setting(obj);
                        }
                    }
                } catch (Exception e) {
                }
            }
        };
        worker.start();
    }
    
        
    Handler mHandler = new Handler() {// 사고발생 여부를 UI에 나타내기 위한 스레드 핸들러
        public void handleMessage(Message msg) {// msg값을 전달 받음
            
            super.handleMessage(msg);
            
            switch (msg.what) {
            case 1:// 로그인 진행 후
                
                //Log.e("2222", "123123");
                output.setText(""+obj);     
                if (output.getText().equals("Login_Success")) {/////////////// 예약화면으로
                    Log.e("3333", "123123");    /////////////// 전환
                    //startActivity(new Intent(AndroidSocketClient.this,RegisterActivity.class));
                    //finish();
                    Intent intentReservation = new Intent(AndroidSocketClient.this, Reservation.class);
                    startActivity(intentReservation);
                    
                    

                } else if (output.getText().equals("Login_Fail")) {// 로그인
                                                                                // 실패시
                                                                                // 아이디
                                                                                // 비번
                                                                                // 입력란
                                                                                // 공백으로
                                                                                // 만듬.
                    input1.setText("");
                    input2.setText("");
                    socket_out.println("1");//////////////// 1을 전송(서버에서 로그인모드)
                }
                
                break;
                
            case 2://///////// 회원가입 페이지로 이동.
                Log.e("3333", "123123");
                Intent intentRegisterActivity = new Intent(AndroidSocketClient.this, RegisterActivity.class);
                startActivity(intentRegisterActivity);
                finish();
                break;
            }
        }
    };

    public void setting(String obj) {
        
        if (obj.equals("2")) {
            mHandler.sendEmptyMessage(2);
        }
        if(obj.equals("Login_Success"))
        {
            mHandler.sendEmptyMessage(1);
        }
        

    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
황황화오항황황 (120 포인트) 님이 2015년 11월 26일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...