2013-01-18 6 views
-2

ProgressDialog를 사용하려고합니다. 내 앱을 실행할 때 진행 대화 상자가 1 초 후에 표시되고 사라집니다.ProgressDialog Android

public class MainActivity extends Activity { 
android.view.View.OnClickListener mSearchListenerListener; 
private ProgressDialog dialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new YourCustomAsyncTask().execute(new String[] {null, null}); 

     } 



    private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> { 

     protected void onPreExecute() { 
      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setMessage("Loading...."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer 
     } 

     protected void doInBackground(String strings) { 
      try { 

      // search(strings[0], string[1]); 

       runOnUiThread(new Runnable() { 
       public void run() { 
        // updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread 
       } 
       }); 

      } catch(Exception e) { 
      } 


     } 

    @Override 
    protected void onPostExecute(Void params) { 
     dialog.dismiss(); 
     //result 

    } 

    @Override 
    protected Void doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 
} 

업데이트 질문 : 나는 여기에 내 코드입니다 .. 내 프로세스의 완료에 보여주고 싶은

내가 전까지 모든 단어가 데이터베이스에 입력되지 않은 ProgressDialogue 상자를 표시 할
 @Override 
    public void onCreate(SQLiteDatabase db) { 
     mDatabase = db; 





      Log.i("PATH",""+mDatabase.getPath()); 



     mDatabase.execSQL(FTS_TABLE_CREATE); 





     loadDictionary(); 
    } 

    /** 
    * Starts a thread to load the database table with words 
    */ 
    private void loadDictionary() { 
     new Thread(new Runnable() { 
      public void run() { 
       try { 
        loadWords(); 
       } catch (IOException e) { 
        throw new RuntimeException(e); 
       } 
      } 
     }).start(); 
    } 

    private void loadWords() throws IOException { 
     Log.d(TAG, "Loading words..."); 


     for(int i=0;i<=25;i++) 

      { //***// 


     final Resources resources = mHelperContext.getResources(); 
     InputStream inputStream = resources.openRawResource(raw_textFiles[i]); 
     //InputStream inputStream = resources.openRawResource(R.raw.definitions); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 

     try { 





      StringBuilder sb = new StringBuilder(); 
      while ((word = reader.readLine()) != null) 
      { 
       sb.append(word); 
      // Log.i("WORD in Parser", ""+word); 
      } 


      String contents = sb.toString(); 
      StringTokenizer st = new StringTokenizer(contents, "||"); 
      while (st.hasMoreElements()) { 
       String row = st.nextElement().toString(); 

       String title = row.substring(0, row.indexOf("$$$")); 
       String desc = row.substring(row.indexOf("$$$") + 3); 
       // Log.i("Strings in Database",""+title+""+desc); 
       long id = addWord(title,desc); 

       if (id < 0) { 
        Log.e(TAG, "unable to add word: " + title); 
       } 
      } 

     } finally { 
      reader.close(); 
     } 

     } 

     Log.d(TAG, "DONE loading words."); 
    } 

. 이 코드는 SQLiteHelper를 확장 한 내부 코드에 있습니다. 그래서 내 내부 클래스에서 ProgressDialogue를 사용하고 백그라운드에서 내 addWords() 메소드를 실행할 수 있습니다.

답변

1

당신은 당신의 doInBackground이

runOnUiThread(new Runnable() { 
       public void run() { 
        // updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread 
       } 
       }); 

을 가질 수 없습니다().

기본 UI 스레드에서 수행되는 다른 작업이있을 때 진행 대화 상자가 우선 순위를 갖지 않습니다. 이 작업은 백그라운드에서 작업이 수행 될 때만 수행됩니다. doInBackground 내 runonUIthread는 도움이되지 않습니다. 그리고 이것은 progressdialog가 몇 초 동안 만 볼 수있는 정상적인 동작입니다.

+0

다음 다른 솔루션 ?? – User42590

+0

나는 같은 상황이었고 아직 어떤 해결책도 찾지 못했습니다. 죄송합니다. –

+0

@Akhter : 왜 updateMapWithResult()를 호출하지 않는가? insidePostExecute 안에? –

1

AsyncTask 클래스에는 두 개의 doInBackground() 메서드가 있습니다. runOnUiThread()을 먼저 doInBackground()에서 제거하고 @Override 주석이있는 doInBackground()으로 이동하십시오.

두 개의 doInBackground() 메쏘드를 실수로 작성했는지 또는 실수로 작성했는지는 알 수 없지만 메쏘드 사이에 혼동을 일으키는 것은 좋지 않습니다. AsyncTask은 첫 번째로 doInBackground()을 호출하지 않으며 @Override 개의 주석이있는 doInBackground()을 호출합니다. 따라서 ProgressDialog은 즉시 null을 반환하므로 1 초 안에 해제됩니다.

+0

편집 할 수 있지만 대화 상자가 단지 1 밀리 초 – User42590

관련 문제