Android 中通信(客户端/服务器)时屏幕暂停

发布于 2024-11-26 19:49:57 字数 1431 浏览 1 评论 0原文

我有一个应用程序,其中有 Google 地图、Google 地图上的位置覆盖以及一个单独的线程,该线程每 30 秒将设备的当前位置发送到服务器。问题是,当线程将位置发送到服务器时,设备的屏幕会停止,直到服务器响应。这是以下代码,

全局对象

private Handler handlerTimer = new Handler();

onCreate 方法中的

 handlerTimer.removeCallbacks(taskUpdateStuffOnDialog );
 handlerTimer.postDelayed(taskUpdateStuffOnDialog , 100);

,这是 taskUpdateStuffOnDialog

    private Runnable taskUpdateStuffOnDialog = new Runnable() 
    {        
        public void run() 
        { 
        try
        { 
        URL url3 = new URL("http://"+ appState.getURL()+"//iLocator/IDForClient.php?reg_no="+ Device_ID[0]);
        HttpURLConnection conn = (HttpURLConnection) url3.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String quote = reader.readLine();
        while (quote != null)
        {
            Device_ID = quote.split("\n");
            quote = reader.readLine();
            bCheckID = true;
        }//End While

        positionOverlay.setID(Device_ID[0]);
        addEvent(Device_ID[0]);

    }//End try
    catch (Exception e) 
    {
        e.printStackTrace();
        Toast.makeText(MainMapActivity.this, "Communication Issue",Toast.LENGTH_LONG).show();       
    }//End catch
handlerTimer.postDelayed(this, 9000);
        }

    };

请告诉我我的代码有什么问题。

I have an application in which there is Google map, location overlays on Google map and a separate thread which send the current location of device to server after every 30 seconds. The problem is that when the thread sends the location to server the screen of device halted until the server respond. Here is the following code,

Global Object

private Handler handlerTimer = new Handler();

In onCreate Method

 handlerTimer.removeCallbacks(taskUpdateStuffOnDialog );
 handlerTimer.postDelayed(taskUpdateStuffOnDialog , 100);

And here is the taskUpdateStuffOnDialog

    private Runnable taskUpdateStuffOnDialog = new Runnable() 
    {        
        public void run() 
        { 
        try
        { 
        URL url3 = new URL("http://"+ appState.getURL()+"//iLocator/IDForClient.php?reg_no="+ Device_ID[0]);
        HttpURLConnection conn = (HttpURLConnection) url3.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String quote = reader.readLine();
        while (quote != null)
        {
            Device_ID = quote.split("\n");
            quote = reader.readLine();
            bCheckID = true;
        }//End While

        positionOverlay.setID(Device_ID[0]);
        addEvent(Device_ID[0]);

    }//End try
    catch (Exception e) 
    {
        e.printStackTrace();
        Toast.makeText(MainMapActivity.this, "Communication Issue",Toast.LENGTH_LONG).show();       
    }//End catch
handlerTimer.postDelayed(this, 9000);
        }

    };

Please tell me what is wrong with my code.

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

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

发布评论

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

评论(2

不必你懂 2024-12-03 19:49:57

问题是,虽然您正在生成一个新线程,但您并没有生成一个新进程。您所做的一切仍然在用户界面进程中,这是阻塞的。您可以在 developer.android.com< 上找到有关该主题的更多信息< /a>.

解决这个问题最快、最简单的方法是使用 IntentService 类。它一次只允许执行一个 HTTP 请求,但会为您解决所有问题。

The problem is that, although you're spawning a new Thread, you aren't spawning a new process. Everything you're doing is still in the user interface process, and that's blocking. You can find more information on the topic on developer.android.com.

The quickest and easiest way to get around this is using the IntentService class. It will only allow one HTTP request to be executed at a time, but will take care of all the problems for you.

红ご颜醉 2024-12-03 19:49:57

尝试使用 AsyncTask 连接到服务器。请参阅此处的示例: http://developer.android.com/reference/android/ os/AsyncTask.html

Try using the AsyncTask for connecting to the Server. See an example here: http://developer.android.com/reference/android/os/AsyncTask.html

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