2012-07-06 68 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

相关问题