开发者

How to stop LocationUpdates in Android

开发者 https://www.devze.com 2023-03-01 01:26 出处:网络
In my application I use GPS to obtain coordinates. I have a button that updates the location. When I update the location using GPS then I create a progressbar. If I stop the progress bar I use

In my application I use GPS to obtain coordinates. I have a button that updates the location. When I update the location using GPS then I create a progressbar. If I stop the progress bar I use

mlocManager.removeUpdates(mlocListener); 

in onCancelListener of the progressbar... When I cancel it then开发者_StackOverflow社区 also some background service runs due to

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

In order to stop it I put my code in a thread. How can I stop the thread since many of the functions (like stop,destroy) are deprecated?


Thread is basically stops now in that way:

  1. You have some variable with synchronize access to it. When you need to stop the thread you set this variable to some value, indicating thread should be stopped.

  2. Thread reads the variable while performing it's execution, and if it sees that variable changes, stops simply returns from run() method.

UPD: It is implemented with methods interrupt() and interrupted() of Thread class.

0

精彩评论

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