protected Dialog onCreateDialog(int id){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
switch(id){
case CONNECTION_DIALOG:
final EditText editIP = new EditText(this);
editIP.setText(sharedPreferences.getString("IP_ADDRESS", ""));
builder.setTitle("Input client IP");
builder.setView(editIP);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(Pattern.matches("^(([2][5][0-5]|[2][0-4][0-9]|[0-1][0-9][0-9]|[0-9][0-9]|[0-9])\\.){3}([2][5][0-5]|[2][0-4][0-9]|[0-1][0-9][0-9]|[0-9][0-9]|[0-9])$",
editIP.getText())){
sharedEdit.putString("IP_ADDRESS", editIP.getText().toString());
sharedEdit.commit();
try {
serviceMessenger.send(Message.obtain(null, AMSService.MSG_CONNECT_AUTHENTICATE));
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), "Error : Cannot send msg to service", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Invalid IPv4 Format", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
break;
case DISCONNECTION_DIALOG:
break;
default:
break;
}
return builder.create();
}
private BroadcastReceiver serviceConnReceiver = new BroadcastReceiver(){
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(AMSServerIntent.ACTION_CONNECT_AUTHENTICATED.equals(action)){
Log.d(TAG, "BR : Authentication Success");
try {
Toast.makeText(getApplicationContext(), "Connection Authenticated", Toast.LENGTH_SHORT).show();
serviceMessenger.send(Message.obtain(null, AMSService.MSG_CONNECT_MAIN));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if(AMSServerIntent.ACTION_CONNECTING.equals(action)){
Log.d(TAG, "BR : Connecting");
pDialog.setMessage("Connecting...");
pDialog.show();
} else if(AMSServerIntent.ACTION_CONNECTED.equals(action)){
Log.d(TAG, "BR : Connection Success");
imgbtnConnect.setBackgroundResource(R.drawable.selector_connected);
isConnected = true;
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
try {
serviceMessenger.send(Message.obtain(null, AMSService.MSG_ON_MAIN));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if(AMSServerIntent.ACTION_DISCONNECTING.equals(action)){
Log.d(TAG, "BR : Disconnecting");
pDialog.setMessage("Disconnecting...");
pDialog.show();
} else if(AMSServerIntent.ACTION_DISCONNECTED.equals(action)){
imgbtnConnect.setBackgroundResource(R.drawable.selector_disconnected);
isConnected = false;
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "Disconnected", Toast.LENGTH_LONG);
Log.d(TAG, "BR : Disconnected");
} else if(AMSServerIntent.ACTION_CONNECTION_FAILED.equals(action)){
Log.d(TAG, "BR : Connecting Failed");
Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
try {
serviceMessenger.send(Message.obtain(null, AMSService.MSG_DISCONNECT_SERVICE));
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), "Error : Cannot send msg to service", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
isConnected = false;
} else{
Log.d(TAG, "BR : This intent does not work");
}
}
};
02-28 11:58:38.874: D/dalvikvm(10049): GC_FOR_ALLOC freed 0K, 18% free 11414K/13767K, paused 19ms, total 19ms
02-28 11:58:38.889: D/dalvikvm(10049): GC_CONCURRENT freed <1K, 18% free 11413K/13767K, paused 11ms+1ms, total 18ms
02-28 11:58:38.899: D/dalvikvm(10049): GC_FOR_ALLOC freed <1K, 18% free 11414K/13767K, paused 7ms, total 7ms
02-28 11:58:38.899: I/dalvikvm-heap(10049): Grow heap (frag case) to 13.066MB for 1440016-byte allocation
02-28 11:58:38.924: D/dalvikvm(10049): GC_FOR_ALLOC freed <1K, 16% free 12820K/15175K, paused 7ms, total 7ms
02-28 11:58:38.924: D/MainActivity(10049): BR : Disconnected
02-28 11:58:38.944: W/InputMethodManagerService(1646): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4158ddd8 attribute=null
02-28 11:58:38.974: I/TAG(10049): Intro : Playing
02-28 11:58:40.114: D/dalvikvm(1646): GC_CONCURRENT freed 291K, 14% free 8544K/9927K, paused 8ms+20ms, total 141ms
02-28 11:58:40.154: E/BinaryDictionaryGetter(1807): Could not find a dictionary pack
02-28 11:58:40.154: E/ActivityThread(1807): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
02-28 11:58:44.339: D/AudioHardware(1403): AudioHardware pcm playback is going to standby.
02-28 11:58:44.339: D/AudioHardware(1403): closePcmOut_l() mPcmOpenCnt: 1
02-28 12:00:00.079: D/AlarmManagerService(1646): Kernel timezone updated to 0 minutes west of GMT
02-28 12:06:46.774: W/ThrottleService(1646): unable to find stats for iface rmnet0
02-28 12:16:46.779: W/ThrottleService(1646): unable to find stats for iface rmnet0
MainActivity에서 Service를 바인드하여 메신저와 브로드캐스트로 상호작용하고,
Service에서는 소켓통신을 하고 있습니다.
위 소스는 MainActivity의 소스 중에 오류가 난다고 추정되는 코드를 가져왔습니다.
처음 Activity가 실행되고 연결 버튼을 누르면 위와 같이 Dialog를 띄우면서 작업을 하고
또 다시 누르면 연결을 닫는 구조로 되어있고,
실제로 처음 실행했을때 한번은 서버와 잘 작업을 하고 연결이 끊어지는데
문제는 다시 연결을 하려고 클릭했을 때 위와같은 에러가 뜨면서 어플리케이션에서는 아무일도 일어나지 않습니다.
서비스의 소켓 연결 문제인가 했더니 관련 Timeout이나 로그도 뜨지 않습니다.
원래 연결 종료 된 것을 UI에 반영하는 부분(ACTION_DISCONNECTED)에
Disconnected라는 메시지를 출력하는 Toast를 출력해야하는데 되지 않는 다는 점과
구글링한 결과로 생각해보니 Service에서 연결 자체의 문제보다는
UI가 제대로 동작을 하지 않는 것 같아서 일단 저 위에 브로드캐스트 리시버의
ACTION_DISCONNECTED부분을 중점적으로 보고 있는데 도무지 감이 잡히는 부분이 없네요. (-_-;;
"BR : Disconnected" 로그는 정상적으로 출력되고 있습니다.
02-28 11:58:38.944: W/InputMethodManagerService(1646): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4158ddd8 attribute=null
02-28 11:58:40.114: D/dalvikvm(1646): GC_CONCURRENT freed 291K, 14% free 8544K/9927K, paused 8ms+20ms, total 141ms
02-28 11:58:40.154: E/BinaryDictionaryGetter(1807): Could not find a dictionary pack
02-28 11:58:40.154: E/ActivityThread(1807): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
이 로그들이 가장 의심스러운데 구글링을 해도 해결방법이 다 달라서...
일단
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
이 부분은 Service와 MainActivity쪽 양 쪽에 넣어봤습니다만 결과는 똑같았습니다.
관련자료나 귀중한 조언 부탁드립니다. (_ _