나는 button
입니다. onclick
이벤트에서 progressDialog
을 실행 한 다음 progressDialog
을로드하는 동안 AsyncTask
을 실행합니다. 안드로이드에 스레드 문제가 있습니다. 도와주세요
방법에서 OnCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
principal_layout = (RelativeLayout) findViewById(R.id.principal_layout);
text_search = (TextView) findViewById(R.id.textView1);
search_button = (Button) findViewById(R.id.button1);
input_song = (EditText) findViewById(R.id.editText1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
search_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
pd = ProgressDialog.show(MainActivity.this, "Working..", "Loading, please wait..", true, false);
}
});
handler.sendEmptyMessage(1);
}});
}
방법 핸들러 (VAR 처리기)
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == 1){
try {
songs = new AsyncTasks().new GetSong(MainActivity.this).execute("mySong","1").get();
} catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
sendEmptyMessage(0);
}
else if(msg.what == 0){
Toast.makeText(MainActivity.this, "Finished process", Toast.LENGTH_SHORT).show();
if(pd != null && pd.isShowing()){
pd.dismiss();
}
}
}
};
코드는 오류를 생성하지만, 옆에 다음과하지 않는:
내가 버튼을 클릭
이 프로그램은 다음 줄을 실행합니다.노래 = 새로운 AsyncTasks()를 새로운 GetSong (MainActivity.this) .execute ("mySong", "1"). get();
일단 실행이 완료되면 최근에 진행 대화 상자가 표시됩니다. 버튼을 클릭하는 정확한 순간을 (지연없이) 보여주는 것이 나의 희망입니다.
onclick을 이미 UI 스레드에서 실행됩니다. 또한이 경우 처리기를 사용할 필요가 없습니다. onClick 메서드에서 AsyncTask를 만들고 시작하기 만하면됩니다. 관련 없음 : 동일한 문제를 가진 사람들이 쉽게 검색 할 수 있도록 질문의 제목을 선택하십시오. – personne3000