티스토리 뷰

반응형

안드로이드 서비스에서 토스트 띄우기 (핸들러)

핸들러를 사용 .

메인엑티비티

public static final int MESSAGE_TOAST = 5; (여러가지의 경우를 만들어 메시지전송가능!)
public static final String TOAST = "toast";
private BluetoothService btService = null;

private final Handler mHandler = new Handler() {

@Override
public void handleMessage(Message msg) {
switch (msg.what) {

case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;

}
super.handleMessage(msg);
}

};

온크리에트 안 에

// BluetoothService 클래스 생성
if(btService == null) {
btService = new BluetoothService(this, mHandler);
}

------------------

BluetoothService .java

public class BluetoothService {

private static final String NAME = "MainActivity";

// Debugging
private static final String TAG = "BluetoothService";
private static final boolean D = true;
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;

// public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
// private ConnectThread mConnectThread;
// private ConnectedThread mConnectedThread;
// private BluetoothAdapter btAdapter;

// 상태를 나타내는 상태 변수
private static final int STATE_NONE = 0; // we're doing nothing
private static final int STATE_LISTEN = 1; // now listening for incoming connections
private static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
private static final int STATE_CONNECTED = 3; // now connected to a remote device

private int mState;
// Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");


private BluetoothAdapter btAdapter;
private Activity mActivity;
private Handler mHandler;

private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;


private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// 디바이스 정보를 얻어서 BluetoothSocket 생성
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}


public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// 연결을 시도하기 전에는 항상 기기 검색을 중지한다.
// 기기 검색이 계속되면 연결속도가 느려지기 때문이다.
btAdapter.cancelDiscovery();
// BluetoothSocket 연결 시도
try {
// BluetoothSocket 연결 시도에 대한 return 값은 succes 또는 exception이다.
Log.d(TAG, "Connect 시도");
mmSocket.connect();
Log.d(TAG, "Connect Success");
} catch (IOException e) {
connectionFailed();
// 연결 실패시 불러오는 메소드
Log.d(TAG, "연결 실패시 불러온다 Connect Fail");

Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.TOAST, "서비스에서 토스트 전송 현재엑티비티에 띄움");
msg.setData(bundle);
mHandler.sendMessage(msg);

// Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT ).show();
// socket을 닫는다.
try {
Log.d(TAG, "소켓 닫아");
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG,
"unable to close() socket during connection failure",
e2);
}
// 연결중? 혹은 연결 대기상태인 메소드를 호출한다.
BluetoothService.this.start();
return;
}
// ConnectThread 클래스를 reset한다.
synchronized (BluetoothService.this) {
mConnectThread = null;
}
// ConnectThread를 시작한다.
connected(mmSocket, mmDevice);
} //run end

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
} //cancel end

}//ConnectThread end

....

...

// Constructors //
//BluetoothService 생성자는 메인이 되는 MainActivity로 부터 Activity와 Handler 값을 받는다.
public BluetoothService(Activity ac, Handler h) {
mActivity = ac;
mHandler = h;

// BluetoothAdapter 얻기
btAdapter = BluetoothAdapter.getDefaultAdapter();
}

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/05   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함