안드로이드 개발 질문/답변 
    (글 수    45,052)
        	
        
        
    @Override
public void onStart(){
super.onStart();
        
Log.e(TAG, "@@ onStart @@");
dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("시간을 가져오는 중입니다...");
dialog.setButton("취소", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
dialog.show();
}
    
private static String MSG = "";
    
protected void Listview(){
try{
final Timer timer = new Timer();
TimerTask time = new TimerTask(){
@Override
public void run(){
try{
MSG = BluetoothService.MESSAGE_DATA;
if(Integer.parseInt(MSG.substring(3, 6)) == 410 && MSG.lastIndexOf("@") != -1){
timer.cancel();
dialog.dismiss();
}
Log.e(TAG, "@@ MSG=" + MSG);
}catch(Exception e){
Log.e(TAG, "@@ error=" + e);
}
}
};
timer.schedule(time, 0, 500);
            
MSG = MSG.substring(6).replace("@", "").trim();
StringTokenizer view = new StringTokenizer(MSG, "|");
            
int count = Integer.parseInt(view.nextToken());
BIN = view.nextToken();
Log.e(TAG, "@@ " + count);
            
items = new ArrayList<String>();
            
/**
* 데이터 없을때 다이얼로그 출력
* count = 0 : 데이터 없음
*/
if(count == 0){
String alertTitle = "알림";
String buttonMessage = "GODUN에 저장된 데이터가 없습니다.";
String buttonYes = "확인";
new AlertDialog.Builder(DataList.this).setTitle(alertTitle).setMessage(buttonMessage).setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(DataList.this, MainView.class);
startActivity(intent);
finish();
}
}).show();
}else{
for(int i = 0; i < count; i++){
tok = view.nextToken();
mYear = "20" + tok.substring(0, 2);
mMonth = tok.substring(2, 4);
mDay = tok.substring(4, 6);
                    
items.add(mYear + "-" + mMonth + "-" + mDay);
}
}
            
listview = (ListView)findViewById(R.id.name);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items);
listview.setAdapter(adapter);
            
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}catch(Exception e){
Log.e(TAG, "@@ error=" + e);
}
        
}
    
@Override
public void onResume(){
super.onResume();
Log.e(TAG, "@@ onResume @@");
Listview();
Log.e(TAG, "@@ onResume END @@");
}
위와 같이 했는데요.
대충 내용은. 블루투스로 데이터를 메인에서 받아서 위 소스로 넘겨 줍니다..
넘겨주면서 변수를 타이머로 500ms로 업데이트 시켜서 완성이 되었을때 캔슬 시켜 줍니다.
문제는 완성이 되고나면 이머 onResume에서 END 로그가 찍혀서. 데이터가 안나타납니다.
제가 알고있는 방법을 다 동원하여 6시간 동안 헤매다가 올려봅니다. 좋은 답변 부탁드리겠습니다.
                                
                            public void onStart(){
super.onStart();
Log.e(TAG, "@@ onStart @@");
dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("시간을 가져오는 중입니다...");
dialog.setButton("취소", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
dialog.show();
}
private static String MSG = "";
protected void Listview(){
try{
final Timer timer = new Timer();
TimerTask time = new TimerTask(){
@Override
public void run(){
try{
MSG = BluetoothService.MESSAGE_DATA;
if(Integer.parseInt(MSG.substring(3, 6)) == 410 && MSG.lastIndexOf("@") != -1){
timer.cancel();
dialog.dismiss();
}
Log.e(TAG, "@@ MSG=" + MSG);
}catch(Exception e){
Log.e(TAG, "@@ error=" + e);
}
}
};
timer.schedule(time, 0, 500);
MSG = MSG.substring(6).replace("@", "").trim();
StringTokenizer view = new StringTokenizer(MSG, "|");
int count = Integer.parseInt(view.nextToken());
BIN = view.nextToken();
Log.e(TAG, "@@ " + count);
items = new ArrayList<String>();
/**
* 데이터 없을때 다이얼로그 출력
* count = 0 : 데이터 없음
*/
if(count == 0){
String alertTitle = "알림";
String buttonMessage = "GODUN에 저장된 데이터가 없습니다.";
String buttonYes = "확인";
new AlertDialog.Builder(DataList.this).setTitle(alertTitle).setMessage(buttonMessage).setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(DataList.this, MainView.class);
startActivity(intent);
finish();
}
}).show();
}else{
for(int i = 0; i < count; i++){
tok = view.nextToken();
mYear = "20" + tok.substring(0, 2);
mMonth = tok.substring(2, 4);
mDay = tok.substring(4, 6);
items.add(mYear + "-" + mMonth + "-" + mDay);
}
}
listview = (ListView)findViewById(R.id.name);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}catch(Exception e){
Log.e(TAG, "@@ error=" + e);
}
}
@Override
public void onResume(){
super.onResume();
Log.e(TAG, "@@ onResume @@");
Listview();
Log.e(TAG, "@@ onResume END @@");
}
위와 같이 했는데요.
대충 내용은. 블루투스로 데이터를 메인에서 받아서 위 소스로 넘겨 줍니다..
넘겨주면서 변수를 타이머로 500ms로 업데이트 시켜서 완성이 되었을때 캔슬 시켜 줍니다.
문제는 완성이 되고나면 이머 onResume에서 END 로그가 찍혀서. 데이터가 안나타납니다.
제가 알고있는 방법을 다 동원하여 6시간 동안 헤매다가 올려봅니다. 좋은 답변 부탁드리겠습니다.
                2011.03.22 23:45:47                            
            
                        
            해결하고 싶은 문제는 지금 TimerTask 를 써서 0.5초에 한번씩 변수를 업데이트 시켜 주는데요..
TimerTask 를 실행 하는 도중에 메소드가 끝나서 TimerTask  아래 이벤트가 일어나지 않고
그냥 끝나는 것이지요...  제 생각엔 TimerTask 말고 그냥 핸들러나 Thread를 써야 할꺼 같은데
핸들러나 Thread도 업데이트를 해줄 수 있는 기능이 있나요?? 보내는쪽에서 계속 푸시를 해야 업데이트 되는거 같은데.. 궁금합니다.
                2011.03.23 10:16:42                            
            
                        
            데이터가 안나타나면 데이터가 있는지 없는지 확인하는 로그를 먼저 찍어보시는게 좋겠네요.
글고 TimerTask로 0.5초 후 무슨 작업을 하고 싶으신지 모르겠지만... 코드상으로 봤을 때 dialog를 dismiss 시키고 싶으신거면... 작업이 종료하고 dissmiss 시키면 안되는 건지요?
글고 Timer에서 이미 스케쥴된 timer task가 돌고 있는데 timer를 cancel하는 것도 이상하군요. 이미 진행되어 돌아가는 timer task에서 schedule을 잡은 timer를 cancel 시키는게 맞는건지?
0.5초에 지속적으로 update를 해줘야 하는 거라면 thread를 deamon 형태로 돌도록 해서 처리해야 할 듯 하구요. ListView()가 완료된 후 dialog를 dismiss() 해줘야 하는 거라면 그냥 코드 마지막 부분에 dialog dismiss()를 해주면 될 것 같고, 0.5 후에 해당 조건으로 dismiss() 되어야 하는 거면 handler의 sendEmptyMessageDelayed() 나 postDelayed()를 사용하면 될 것 같네요.
                                
            글고 TimerTask로 0.5초 후 무슨 작업을 하고 싶으신지 모르겠지만... 코드상으로 봤을 때 dialog를 dismiss 시키고 싶으신거면... 작업이 종료하고 dissmiss 시키면 안되는 건지요?
글고 Timer에서 이미 스케쥴된 timer task가 돌고 있는데 timer를 cancel하는 것도 이상하군요. 이미 진행되어 돌아가는 timer task에서 schedule을 잡은 timer를 cancel 시키는게 맞는건지?
0.5초에 지속적으로 update를 해줘야 하는 거라면 thread를 deamon 형태로 돌도록 해서 처리해야 할 듯 하구요. ListView()가 완료된 후 dialog를 dismiss() 해줘야 하는 거라면 그냥 코드 마지막 부분에 dialog dismiss()를 해주면 될 것 같고, 0.5 후에 해당 조건으로 dismiss() 되어야 하는 거면 handler의 sendEmptyMessageDelayed() 나 postDelayed()를 사용하면 될 것 같네요.











질문의 뜻 이해를 잘 못하겠어요... 다시한번 알려주세요..