后台服务是否会在 Web 服务完成执行之前挂起 UI?

发布于 2025-01-08 09:12:22 字数 4506 浏览 2 评论 0原文

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

下面是我的服务的代码,

公共类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;
    }
}

}

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

I have created a webservice class lokks like below, in with in the "onCreate" method of the service i Have called my webservice which takes around 45 seconds to complete its execution for that time my UI gets black that means it Hangs upto the execution of the web service,

below is the code of my service,

public class productService extends 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;
    }
}

}

Anyone please help me to sort out the probelm,
I am Stuck Here,
Thanks in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-01-15 09:12:22

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

For Background Services use the AsyncTask which creates background threads so doesn't effect your main UI.
Here is the Code:

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

For more details: See this one
http://developer.android.com/reference/android/os/AsyncTask.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文