简体   繁体   English

我应该如何设置我的Android Google Maps应用程序?

[英]How should I set up my Android Google maps application?

I have been working on an Android application that uses Google maps and then runs some lengthy network intensive operations in the background. 我一直在研究使用Google地图的Android应用程序,然后在后台运行一些冗长的网络密集型操作。 Currently, I am using a thread to run them on and they take anywhere from 30 seconds to 7 minutes to finish. 目前,我正在使用一个线程来运行它们,它们需要30秒到7分钟才能完成。 After watching a few courses on Pluralsight about AsyncTasks and Background services, I now know that threads should ideally not be used for anything taking more than a few seconds. 在观看了有关AsyncTasks和Background服务的有关Pluralsight的几门课程之后,我现在知道,理想情况下,不要将线程用于耗时超过几秒钟的事情。 I am now altering my solution to run live with GPS rather than taking several minutes to perform the operations. 现在,我正在更改解决方案以与GPS一起实时运行,而不是花费几分钟来执行操作。 The goal is to update an array every OnLocationChanged event. 目标是在每个OnLocationChanged事件中更新一个数组。

I am having trouble in thinking about how I could alter a global array every OnLocationChanged event while also accessing it from the UI main thread. 我在考虑如何在从UI主线程访问它的同时,如何更改每个OnLocationChanged事件的全局数组时遇到了麻烦。 What is my best option for accomplishing this? 我最好的选择是什么? Would I be able to use a process or AsynTask to accomplish this or would I need to go client/server route? 我将能够使用进程或AsynTask来完成此任务,还是需要走客户机/服务器路由? Where would the OnLocationChanged be called? OnLocationChanged在哪里被调用?

First off, onLocationUpdated is called on the UI thread. 首先,在UI线程上调用onLocationUpdated。 So you don't have to worry about multithreading there. 因此,您不必担心那里的多线程。

Secondly- if you have a variable that needs to be touched by two threads you just use a semaphore and take it before you need to access it on each thread, and release it when done. 其次,如果您有一个需要两个线程接触的变量,则只需使用一个信号量,然后在需要访问每个线程上的信号量之前使用它,并在完成后释放它。 Make sure to keep that block of code as small and quick as possible. 确保使该代码块尽可能小且快速。 There's more advanced stuff you can do for high performance needs, but that's good enough for 99% of code. 您可以为满足高性能需求而做更多高级的事情,但这足以满足99%的代码。

Thirdly- as I mentioned in my comment, your understanding of threading is wrong. 第三,正如我在评论中提到的那样,您对线程的理解是错误的。 The UI thread should not be used for more than is needed. UI线程的使用时间不应超过所需的时间。 AsyncTasks should not run for more than a few seconds (as there's a single thread they run on by default, so running long would block other requests). AsyncTasks的运行时间不得超过几秒钟(因为默认情况下它们运行在单个线程上,因此长时间运行会阻止其他请求)。 But a Thread can run as long as it needs to, and should be used if it needs long term background processing. 但是,线程可以运行所需的时间,如果需要长期的后台处理,则应使用该线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google Maps API 在我的 Google Maps Android 应用程序中添加路线? - How can I add a route in my Google Maps Android application using Google Maps API? 我的Android应用程序中的google maps - google maps in my android application 打开我的应用程序时,Android Studio Google Maps如何将相机重定向到我的当前位置 - Android Studio Google Maps how to redirect camera to my current location when i open my application 如何在Android Studio中设置Google Maps Utility Api? - How to set up Google Maps Utility Api in Android Studio? 如何在Android应用程序com.google.android.gms.maps.GoogleMap中导入此程序包(库) - how can i import this packages (Libraries) in my android application com.google.android.gms.maps.GoogleMap 如何将Google Maps时区API密钥限制为Android应用程序? - How do I restrict my Google Maps Time Zone API key to an Android application? 是否可以像Waze和Google Map一样将地图应用程序添加到“ Android Auto”? - Can I add my Maps application to “Android Auto” just like how Waze and Google Map does? 如何将我的Android应用程序链接到另一个内置应用程序(例如Google Maps)? - How do i link my android app to another built in application like google maps? 谷歌地图无法显示在我的应用程序中 - google maps cant appear in my application android 谷歌地图未加载到我的Android应用程序中 - google maps not loading in my android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM