1

我已经创建了一个如下所示的 web 服务类 lokks,在服务的“onCreate”方法中,我调用了我的 web 服务,这需要大约 45 秒才能完成它的执行,因为那时我的 UI 变黑了,这意味着它挂起执行网络服务,

以下是我的服务代码,

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

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

}

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

4

1 回答 1

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

于 2012-02-22T13:20:56.743 回答