BlackBerry - 应用程序在后台线程执行时冻结

时间:2013-12-09 09:03:19

标签: multithreading blackberry freeze timertask thread-sleep

我有一个BlackBerry App,当按钮的状态设置为ON时,它会通过Web服务发送数据。当按钮为ON时,启动计时器,该计时器以固定间隔在后台连续运行。 HttpConnection的方法调用如下:

if(C0NNECTION_EXTENSION==null)
{
Dialog.alert("Check internet connection and try again");
return;
}
else
{
confirmation=PostMsgToServer(encryptedMsg); 
}

方法PostMsgToServer如下:

public static String PostMsgToServer(String encryptedGpsMsg)  { 

            //httpURL=  "https://prerel.track24c4i.com/track24prerel/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";
        httpURL=  "https://t24.track24c4i.com/track24c4i/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";
        //httpURL=  "http://track24.unit1.overwatch/track24/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";

            try { 
                String C0NNECTION_EXTENSION = checkInternetConnection();
                if(C0NNECTION_EXTENSION==null)
                {
                    Dialog.alert("Check internet connection and try again");
                    return null;
                }
                else
                {

                    httpURL=httpURL+C0NNECTION_EXTENSION+";ConnectionTimeout=120000";
            //Dialog.alert(httpURL); 
            HttpConnection httpConn; 
            httpConn = (HttpConnection) Connector.open(httpURL); 
            httpConn.setRequestMethod(HttpConnection.POST); 
            DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream()); 
            byte[] request_body = httpURL.getBytes(); 
            for (int i = 0; i < request_body.length; i++) { 
            _outStream.writeByte(request_body[i]); 
            } 
            DataInputStream _inputStream = new DataInputStream( 
            httpConn.openInputStream()); 
            StringBuffer _responseMessage = new StringBuffer(); 
            int ch; 
            while ((ch = _inputStream.read()) != -1) { 
            _responseMessage.append((char) ch); 
            } 
            String res = (_responseMessage.toString()); 
            responce = res.trim(); 


            httpConn.close(); 
                }
            }catch (Exception e) { 
            //Dialog.alert("Connection Time out"); 
            } 

            return responce;
            } 

我的问题:每当调用该方法时,应用程序都会冻结,即每当计时器必须执行并向网络服务发送数据时,应用程序就会冻结 - 有时会持续几秒钟相当长的时间应用于用户,就像手机挂了一样。这可以解决吗?请帮助!!

1 个答案:

答案 0 :(得分:2)

您正在事件线程上运行网络操作 - 即处理应用程序的Ui交互的相同线程。网络是一种阻止操作,因此有效地阻止了您的UI操作。不推荐在事件线程上执行此操作,说实话,我很惊讶它不会导致应用程序被终止,因为这通常是操作系统将要执行的操作,如果它认为应用程序已阻止事件线程。

解决此问题的方法是使用单独的线程开始网络处理。这通常是容易的部分,困难的部分是

  1. 阻止用户在等待时执行任何其他操作 回应(假设你需要这样做)
  2. 使用您的网络结果更新用户界面 处理
  3. 我认为本主题中讨论了第二个问题: adding-field-from-a-nonui-thread-throws-exception-in-blackberry

    由于看起来你正在尝试在后台定期进行此更新,我认为第一个不是问题, - 为了完整性,你可以搜索SO以获得答案,包括这个: blackberry-please-wait-screen-with-time-out

    此处有关于事件线程的更多信息:

    Event Thread

相关问题