【问题标题】:Background Service Hangs UI upto the time web service finish its execution?后台服务在 Web 服务完成执行之前挂起 UI?
【发布时间】:2012-02-22 13:08:00
【问题描述】:

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

下面是我的服务代码,

公共类 productService 扩展 Service {

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

}

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

【问题讨论】:

    标签: android service background-service


    【解决方案1】:

    对于后台服务,请使用创建后台线程的 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

    【讨论】:

    • 调用这个方法为:new uploadFile().execute(string, string, string);
    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2022-01-19
    • 2015-11-24
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多