2012-01-31 8 views
1

URLを読み込むために以下のコードを試してみます。読み込みダイアログを表示するには

URL url = new URL(urlstr); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setConnectTimeout(10000); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 
InputStream is = connection.getInputStream(); //spend lots of time 

行番号InputStream is = connection.getInputStream();はしばらく時間がかかります。 ロード中に読み込みダイアログを表示したいと思います。 できますか?

AActivityでは、以下のコードでBActivityを呼び出します。

Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Window w = MyGroup.group.getLocalActivityManager().startActivity("BActivity", intent); 
View view = w.getDecorView(); 
MyGroup.group.setContentView(view); 

そして、BActivityはロードURLであり、情報を抽出します。 ロードコードはonCreate()にあります。

回答コードを試してみます。エラーUnable to add window -- token [email protected] is not valid; is your activity running?が表示されます。

答えて

3

を参照してください:downlodingため

  new GetTask(this).execute();//taken object for asyntask class. 



    class GetTask extends AsyncTask<Object, Void, String> { 
    { 
     ProgressDialog progressDialog; 
     void GetTask(Context cntxt) 
     { 

     } 
     @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     mDialog = new ProgressDialog(cntxt); //taking object for progress dialog 
     mDialog.setMessage("Please wait..."); 
     mDialog.show(); //Displaying progressDialog 
    } 

    @Override 
    protected String doInBackground(Object... params) { 

     //do the background process 

     return ""; you can return string value 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     if (mDialog != null) { 
      mDialog.dismiss();//close the progress Dialog 
     } 

    } 
     } 
+0

は、私たちがもし(mDialog.isShowing()){進捗ダイアログ を閉じる// mDialog.dismiss()} を使うべきだと思います。 dismiss(); //進行状況を閉じるダイアログ } – Akram

+0

「ウィンドウを追加できません」というメッセージが表示されます。私は自分の問題を編集しています。 – brian

1

ProgressDialogが必要です。 OnCreate関数で

を次のようにあなたは進捗ダイアログを示すaysntaskで達成することができ、このlink

1

の進行状況]ダイアログ

private ProgressDialog dialog; 
    public void showProgress() { 
     dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Please wait"); 
     dialog.show(); 

    } 

使用非同期タスクを...

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 
      String response = ""; 
      for (String url : urls) { 
       //Do your downloading task 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
       dialog.dismiss(); 
      } 
     } 

コールプログレスダイアログのコンテキストを初期化し、ダイアログでそのコンテキストを使用するようにDownloadWebPageTaskでコンストラクタを使用してダウンロードタスク

showProgress(); 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://www.url.com" }); 
+0

「ウィンドウを追加できません」というメッセージが表示されます。私は自分の問題を編集しています。 – brian

+0

'This'を' YourActivityName.this'に置き換えてください。 – Sandy

+0

それでも動作しません。 – brian

1

を実行する前に。

であなたのクラスを使用してください。dialog = new ProgressDialog(yourclass.this);代わりに (!mDialog = NULL){ 場合mDialogの ;

関連する問題