2012-02-22 44 views
1

我已經創建了一個類似下面的webservice類lokks,在服務的「onCreate」方法中我已經調用了我的webservice,這需要大約45秒來完成它的執行,那時我的UI變黑了,這意味着它掛起Web服務的執行,後臺服務將UI掛起到Web服務完成執行的時間?

下面

是我服務的代碼,

公共類productService擴展服務 {

private static Context _pctx; 
static Vector _productsAll = null; 


public static void getInstance(Context context) throws Exception 
{ 
    if (_pctx == null) 
    { 
     _pctx = context; 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate() 
{ 
    try 
    { 
     LoadAllProducts(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 



@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("LocalService", "Received start id " + startId + ": " + intent); 


    return START_REDELIVER_INTENT; //  21 sec 

} 

@Override 
public void onDestroy() 
{ 
    _productsAll= null; 
} 



private void LoadAllProducts() throws Exception 
{ 

    _productsAll = new Vector(); 
    Exception e = null;  


    WebResponse myResponse = DataService.GetData("$PR$" , _pctx); 
    if (Helper.getBoolValueFromString(myResponse.Success)) 
    { 
     saveMDBData(myResponse.Response); 
    } 
    else 
    { 
     e = new Exception(myResponse.Response.toString()); 
    } 
    //cats = null; 

    if (e != null) { 
     throw e; 
    } 
} 

public static void saveMDBData(StringBuffer pMDBData) 
{ 
    Vector Rows; 
    Vector Cols; 

    int iRow = 0; 

    if (pMDBData != null) 
    { 
     if (!pMDBData.toString().trim().equals("")) 
     { 
      Rows = Helper.getRowsNew(pMDBData); 

      if (Rows != null) 
      { 
       for (iRow = 0; iRow < Rows.size(); iRow++) 
       { 
        if (!((String) Rows.elementAt(iRow)).trim().equals("")) 
        { 
         Cols = Helper.SplitMultiCharDelimiters((String) Rows.elementAt(iRow), Helper.FIELDDELIMITERS); 
         assignMDBData(Cols); 
        } 
       } 

      } 
     } 
    } 
    Rows = null; 
    Cols=null; 
} 

private static void assignMDBData(Vector pCols) 
{ 
    Product myProduct = null; 

    if (pCols != null) 
    { 
     //Create new setting instance 
     //myProduct = new Product(); 

      myProduct = new Product(); 

     //assign values 
     myProduct.Id = Helper.getIntValue((String)pCols.elementAt(0)); 
     myProduct.PartNumber = (String)pCols.elementAt(1); 
     myProduct.Description = (String)pCols.elementAt(2); 
     myProduct.IdCategory = Helper.getIntValue((String)pCols.elementAt(3)); 
     myProduct.Ideal = Helper.getIntValue((String)pCols.elementAt(4)); 
     myProduct.Taxable = Helper.getBoolValueFromString((String)pCols.elementAt(5)); 
     myProduct.Discountable = Helper.getBoolValueFromString((String)pCols.elementAt(6)); 
     myProduct.LotSize = Helper.getIntValue((String)pCols.elementAt(7)); 
     myProduct.RetailPrice = Helper.getDoubleValue((String)pCols.elementAt(8)); 
     myProduct.ListPrice = Helper.getDoubleValue((String)pCols.elementAt(9)); 
     myProduct.TotalOnHand = Helper.getIntValue((String)pCols.elementAt(10)); 
     myProduct.TotalOnOrder = Helper.getIntValue((String)pCols.elementAt(11)); 
     myProduct.IsPrepack = Helper.getBoolValueFromString((String)pCols.elementAt(12)); 
     //myProduct.Breakdown = (String)pCols.elementAt(13); 
     myProduct.NoInventory = Helper.getBoolValueFromString((String)pCols.elementAt(13)); 
     myProduct.IsCollection = Helper.getBoolValueFromString((String)pCols.elementAt(14)); 
     myProduct.Followup = Helper.getIntValue((String)pCols.elementAt(15)); 
     myProduct.PctDiscount = Helper.getDoubleValue((String)pCols.elementAt(16)); 
     myProduct.IdGroup = Helper.getIntValue((String)pCols.elementAt(17)); 
     myProduct.Points = Helper.getIntValue((String)pCols.elementAt(18)); 
     myProduct.IsVitamin = Helper.getBoolValueFromString((String)pCols.elementAt(19)); 
     myProduct.PusChange = Helper.getIntValue((String)pCols.elementAt(20)); 
     myProduct.MovedToCloseout = Helper.getDateDataSync((String)pCols.elementAt(21)); 
     myProduct.OnHandDelta = Helper.getIntValue((String)pCols.elementAt(24)); 

     //save processed setting to persistent collection 
     _productsAll.addElement(myProduct); 

     //release saved setting in)stance 
     myProduct = null; 
    } 
} 

}

任何人都請幫我理清這個問題, 我困在這裏, 在此先感謝!

回答

0

對於後臺服務使用AsyncTask創建後臺線程,所以不影響您的主UI。 這裏是代碼:

public class DownloadData extends AsyncTask<String, String, String> { 
    @Override 
    public void onPreExecute() { 


      // Do Some Task before background thread runs 



    } 

    @Override 
    protected String doInBackground(String... arg0) { 


        // To Background Task Here 



     return null; 
    } 

    @Override 
    protected void onProgressUpdate(String... progress) { 

     // publish progress here 
    } 

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

        // Do some Task after Execution 
} 
    } 

有關詳細信息:請參閱本一個 http://developer.android.com/reference/android/os/AsyncTask.html

+0

調用此方法爲: 新uploadFile()執行(字符串,字符串,字符串); – 2012-02-22 13:23:54