开发者

threads or service

I need to have gps running all the time in my application, but I don´t know if it is better throw it like a thread inside of the activity or if I should creat a Service and then in the Service, throw the thread

Can anybody help me?? Thank you!!

P.D: Sorry for my English


My answer doesn´t lie in how my ap开发者_C百科plication will work because I want my application goes to the background and it works. I'm going to try to explain what my application consists: It has one activity that starts one service with the gps, the locations from gps are compared with others locations that it has in a file. When a condition is true, then it starts another service running a multimedia player.

So if I am watching another application or if I have the device in my pocket, I want my application continues running.


I suppose your answer would lie in how your application will work. If you want to receive the GPS data even after your application has been pushed to the background by pressing home button or back button then you should have it in a service since the life cycle of a service is different to that of an Activity.

If the GPS data is only required within your application and you don't need it ones the application is closed then threads are a better option as Kavitesh has mentioned, threads are a much quicker and easier approach


Could you explain some more please? Because we need to know what your application is to do. When your app is always visible, you don't need a thread or a service, you should implement a listener and register that to the LocationManager.

When you application should go to the background, then you should implement a Service.


Two things which you need to consider:

  1. If the GPS information is required only for your application then have a thread which pools GPS information every second or interval set by you. This way your application gets the information from GPS and you dont have to complicate things by making a service. Remember, By making a thread within your application you need to start your application so that you can obtain GPS information.
  2. Now lets say you want GPS information for other applications also. Then using a service is a good idea. That way other applications can also obtain GPS information and use in their application. Ofcourse, this wont happen magically that all application can get the GPS information by running the service. You would have to write code and provide interfaces so that all other applications along with your application know how to interact with the service.

Now using a thread in application is quicker and neat if it meets your requirement. However, like i mentioned your application needs to start before using the GPS information.

With Service, you can even run the service at start up. So you need not start your application to start the GPS. However, unless required go for thread in the application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜