2012-07-06 26 views
0

這裏的問題是,在此之前我把所有的代碼下的onCreate();但加載時間超過10秒,它將顯示空白屏幕,直到屏幕完全加載。所以我將一些代碼移到了doInBackground。但我面對它的onCreate(下不會發生一些問題),讓我們看看現在的代碼:的AsyncTask沒有功能以及doinbackground

String photoURL1; 
@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.app_details); 

    tvDescription = (TextView)findViewById(R.id.tvDescription); 

    tvTitle = (TextView)findViewById(R.id.tvTitle); 
    tvDeveloper = (TextView)findViewById(R.id.tvDeveloper); 

    rbRating = (RatingBar)findViewById(R.id.rbRating); 

    ivLogo = (ImageView)findViewById(R.id.ivLogo); 
    ivPhoto1 = (ImageView)findViewById(R.id.ivPhoto1); 
    ivPhoto2 = (ImageView)findViewById(R.id.ivPhoto2); 
    ivPhoto3 = (ImageView)findViewById(R.id.ivPhoto3); 

    new loadPage().execute(null, null, null); 
} 

public class loadPage extends AsyncTask<String, Integer, Void> { 
     private ProgressDialog pdia; 

    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     pdia = new ProgressDialog(AppDetails.this); 
     pdia.setMessage("Loading..."); 
     pdia.show(); 
     } 

    @Override 
    protected Void doInBackground(String... aurl) { 
     SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME); 

     Request.addProperty("title", getIntent().getExtras().getString("xdaTitle")); 

     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     soapEnvelope.dotNet = true; 
     soapEnvelope.setOutputSoapObject(Request); 

     AndroidHttpTransport aht = new AndroidHttpTransport(URL); 

     try 
     { 
      aht.call(SOAP_ACTION, soapEnvelope); 
      SoapObject resultString = (SoapObject) soapEnvelope.getResponse(); 

      for(int i =0; i<resultString.getPropertyCount(); i++) 
      { 
       SoapObject array = (SoapObject) resultString .getProperty(i); 

       title = array.getProperty(1).toString(); 
       developer = array.getProperty(3).toString();  

         //load images  
       photoURL1 = array.getProperty(5).toString(); //get logo url 


       BitmapFactory.Options bmOptions; 
       bmOptions = new BitmapFactory.Options(); 
       bmOptions.inSampleSize = 1;   

      } 
     } 
     catch(Exception e){            
     }   

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress2) { 

    } 


     @Override 
     protected void onPostExecute(Void unused) { 
      tvTitle.setText(title); 
      tvDeveloper.setText(developer); 
      BitmapFactory.Options bmOptions; 
      bmOptions = new BitmapFactory.Options(); 
      bmOptions.inSampleSize = 1; 

      Bitmap bmLogo = LoadImage(photoURL1, bmOptions); 
      ivLogo.setImageBitmap(bmLogo); 
      pdia.dismiss(); 
     } 

這是問題所在。

  • 我tvTitle只顯示前3個字符,tvDeveloper僅顯示7〜8個字符(tvtitle和tvDeveloper是一個TextView)
  • 力關閉,如果我從doInBackground移動圖像加載器onPostExecute發生。

=======更新:我應該在哪裏停車這個代碼? =================

MyI = new Intent(Intent.ACTION_VIEW); 
      MyI.setAction(android.content.Intent.ACTION_VIEW); 
      MyI.setDataAndType(Uri.parse("file:///sdcard/MaxApps/" + apkURL.toString()), "application/vnd.android.package-archive"); 

      MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0); 
      MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent intent = new Intent(getApplicationContext(), AppDetails.class); 
      intent.putExtra("xdaTitle", title); 
      final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 

      notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis()); 
      notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
      notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.downloadapk); 
      notification.contentIntent = pendingIntent; 
      notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save); 
      notification.contentView.setTextViewText(R.id.tvText, "Downloading " + apkURL); 
      notification.contentView.setViewVisibility(R.id.pbStatus, View.VISIBLE); 
      notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);   
      notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE); 
+0

發表您的佈局XML文件還,這樣文本截斷還可以檢查 – sunil 2012-07-06 07:03:09

+0

究竟做了什麼是你的新代碼? – user370305 2012-07-06 07:14:33

+0

我敢打賭,還有另外一個問題,使這個主題很容易理解/清楚了,我已經打開了一個新問題:http://stackoverflow.com/questions/11357721/notification-bar-in-asynctask,THX的幫助思考:) – melvintcs 2012-07-06 07:29:47

回答

1

你不能在後臺線程改變UI。

的的AsyncTask有一個更好的選擇,更新,同時下載的用戶界面,這是使用publishProgress()

你需要在你的代碼的一些變化這樣來實現它。

 @Override 
     protected Void doInBackground(String... aurl) { 

       for(int i =0; i<resultString.getPropertyCount(); i++) 
       {      
        String logo_URL = array.getProperty(5).toString(); //get logo url 
        bmLogo = LoadImage(logo_URL, bmOptions); 
        String photo1_URL = array.getProperty(6).toString(); 
        bmPhoto1 = LoadImage(photo1_URL, bmOptions); 
        String photo2_URL = array.getProperty(7).toString(); 
        bmPhoto2 = LoadImage(photo2_URL, bmOptions); 
        String photo3_URL = array.getProperty(8).toString(); 
        bmPhoto3 = LoadImage(photo3_URL, bmOptions); 
        publishProgress(null); 
       } 
      } 
      catch(Exception e){            
      }    

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... progress2) { 
      ivLogo.setImageBitmap(bmLogo); 
      ivPhoto1.setImageBitmap(bmPhoto1); 
      ivPhoto2.setImageBitmap(bmPhoto2); 
      ivPhoto3.setImageBitmap(bmPhoto3); 
     } 
2

其實,

ivLogo.setImageBitmap(bmLogo); 
ivPhoto1.setImageBitmap(bmPhoto1); 
ivPhoto2.setImageBitmap(bmPhoto2); 
ivPhoto3.setImageBitmap(bmPhoto3); 

這些線doInBackground()事業的問題。因爲您正嘗試從工作線程(非UI線程)更新MainUI線程。

你不能從AsyncTaskdoInBackground()更新MainUI所以不如把這個UI更新代碼的AsyncTaskonPostExecute()

+0

讓您的AsyncTask一個位圖陣列填寫doInBackground(那些陣列)和用於onPostExcute(即數組),這樣你可以將其設置爲ImageViews .. – user370305 2012-07-06 06:36:59

+0

返回doInBackground該數組所以你會得到它作爲onPostExecute參數( ) – jpa 2012-07-06 06:46:54

+0

是的,我知道。我所做的是我將URL存儲到'doInBackground'下的字符串中,並將該URL傳遞給onPostExecute()(就像我對tvTitle所做的那樣)它使系統強制執行。我已經更新了代碼,請看看:) – melvintcs 2012-07-06 06:48:36

相關問題