2012-11-14 23 views
0

可能重複:
ProgressDialog not showing up in activity對話框不是從另一個類顯示

在我的主類我稱之爲 新的自動更新()的checkForUpdate(本)。

這是動作

public void checkForUpdate(Context context) { 
     // check when last checked. unless overridden check once a day. 

     // get current version 
     int resID = context.getResources().getIdentifier("current_version", 
       "string", context.getPackageName()); 
     currentVersion = context.getResources().getString(resID); 

     // check for connection 

     // now compare versions 
     if (currentVersion.equals(serverVersion) == false) { 
      ProgressDialog pDialog = new ProgressDialog(context); 
      pDialog.setMessage("DO NOT ROTATE YOUR DEVICE. /nI am upgrading your version, press OK then INSTALL in a minute. Please wait...."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
      try { 

       URL url = new URL(prefs.getString("server_address", null) 
         + "/updates/eduDroid.apk"); 
       HttpURLConnection c = (HttpURLConnection) url.openConnection(); 

       String PATH = Environment.getExternalStorageDirectory() 
         + "/download/"; 
       File file = new File(PATH); 
       file.mkdirs(); 
       File outputFile = new File(file, "eduDroid.apk"); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       InputStream is = c.getInputStream(); 

       byte[] buffer = new byte[1024]; 
       int len1 = 0; 
       while ((len1 = is.read(buffer)) != -1) { 
        fos.write(buffer, 0, len1); 
       } 
       fos.close(); 
       is.close(); 

       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.fromFile(new File(Environment 
         .getExternalStorageDirectory() 
         + "/download/" 
         + "file.apk")), 
         "application/vnd.android.package-archive"); 
       context.startActivity(intent); 

      } catch (IOException e) { 
       Toast.makeText(context, "Update error!", Toast.LENGTH_LONG) 
         .show(); 
      } 
      pDialog.dismiss(); 
     } 

    } 

的問題是,它需要很長時間下載更新的版本。在這段時間內,用戶留下了空白屏幕。我想向他們展示對話框,但它沒有顯示。請幫助

+0

wtf你會得到resID嗎?爲什麼不使用getString(R.string.current_version)? – njzk2

+1

使用asynctask或activity.showDialog()。你正在嘗試在UI線程之外顯示一個對話框,這是禁止的 – njzk2

+0

@ njzk2謝謝指出!它得到了這個名稱的變體,但現在改變了。 – RuAware

回答

0

您可以使用AsyncTask做你的網絡的東西在後臺:

if (currentVersion.equals(serverVersion) == false) { 
     new AsyncTask<Void, Void, Void>() { 

      private ProgressDialog pDialog; 

      @Override 
      protected void onPreExecute() { 
       pDialog = new ProgressDialog(context); 
       pDialog.setMessage("DO NOT ROTATE YOUR DEVICE. /nI am upgrading your version, press OK then INSTALL in a minute. Please wait...."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 
       pDialog.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... params) { 
       try { 

        URL url = new URL(prefs.getString("server_address", 
          null) + "/updates/eduDroid.apk"); 
        HttpURLConnection c = (HttpURLConnection) url 
          .openConnection(); 

        String PATH = Environment.getExternalStorageDirectory() 
          + "/download/"; 
        File file = new File(PATH); 
        file.mkdirs(); 
        File outputFile = new File(file, "eduDroid.apk"); 
        FileOutputStream fos = new FileOutputStream(outputFile); 

        InputStream is = c.getInputStream(); 

        byte[] buffer = new byte[1024]; 
        int len1 = 0; 
        while ((len1 = is.read(buffer)) != -1) { 
         fos.write(buffer, 0, len1); 
        } 
        fos.close(); 
        is.close(); 

        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.fromFile(new File(Environment 
          .getExternalStorageDirectory() 
          + "/download/" 
          + "file.apk")), 
          "application/vnd.android.package-archive"); 
        context.startActivity(intent); 

       } catch (IOException e) { 
        Toast.makeText(context, "Update error!", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       pDialog.dismiss(); 
      } 
     }.execute(); 
    } 
+0

感謝這似乎工作。 – RuAware

0

嘗試,而不是(這個)使用處理程序或可運行顯示進度對話框和(ActivityName.this),同時通過上下文對話。

相關問題