后台服务挂起UI直到web服务完成执行



我创建了一个类似于下面的web服务类lokks,在服务的"onCreate"方法中,我调用了我的web服务,大约需要45秒才能完成执行,在这段时间里,我的UI变黑,这意味着它挂起了web服务的执行,

下面是我的服务的代码

公共类产品服务扩展服务{

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;
    }
}

}

任何人请帮我解决这个问题,我被困在这里,提前感谢!

对于后台服务,请使用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

最新更新