1111
1212import com .blankj .androidutilcode .App ;
1313import com .blankj .utilcode .utils .LocationUtils ;
14+ import com .blankj .utilcode .utils .ThreadPoolUtils ;
1415import com .blankj .utilcode .utils .ToastUtils ;
1516
1617/**
2324 */
2425public class LocationService extends Service {
2526
26- private boolean isSuccess ;
27- private LocationUtils locationUtils ;
27+ private boolean isSuccess ;
28+ private LocationUtils locationUtils ;
2829 private String lastLatitude = "loading..." ;
2930 private String lastLongitude = "loading..." ;
3031 private String latitude = "loading..." ;
@@ -33,36 +34,11 @@ public class LocationService extends Service {
3334 private String locality = "loading..." ;
3435 private String street = "loading..." ;
3536 private OnGetLocationListener mOnGetLocationListener ;
36- private Thread mThread ;
3737
3838 public void setOnGetLocationListener (OnGetLocationListener onGetLocationListener ) {
3939 mOnGetLocationListener = onGetLocationListener ;
4040 }
4141
42- @ Override
43- public void onCreate () {
44- super .onCreate ();
45- mThread = new Thread (new Runnable () {
46- @ Override
47- public void run () {
48- Looper .prepare ();
49- locationUtils = new LocationUtils (App .getInstance ());
50- isSuccess = locationUtils .init (1000 , 0 , mOnLocationChangeListener );
51-
52- if (isSuccess ) {
53- ToastUtils .showShortToastSafe (App .getInstance (), "init success" );
54- } else {
55- ToastUtils .showShortToastSafe (App .getInstance (), "init fail" );
56- if (mOnGetLocationListener != null ) {
57- mOnGetLocationListener .getLocation ("unknown" , "unknown" , "unknown" , "unknown" , "unknown" , "unknown" , "unknown" );
58- }
59- }
60- Looper .loop ();
61- }
62- });
63- mThread .start ();
64- }
65-
6642 private LocationUtils .OnLocationChangeListener mOnLocationChangeListener = new LocationUtils .OnLocationChangeListener () {
6743 @ Override
6844 public void getLastKnownLocation (Location location ) {
@@ -80,18 +56,12 @@ public void onLocationChanged(final Location location) {
8056 if (mOnGetLocationListener != null ) {
8157 mOnGetLocationListener .getLocation (lastLatitude , lastLongitude , latitude , longitude , country , locality , street );
8258 }
83- // 开启新线程来获取地理位置
84- // new Thread(new Runnable() {
85- // @Override
86- // public void run() {
8759 country = locationUtils .getCountryName (Double .parseDouble (latitude ), Double .parseDouble (longitude ));
8860 locality = locationUtils .getLocality (Double .parseDouble (latitude ), Double .parseDouble (longitude ));
8961 street = locationUtils .getStreet (Double .parseDouble (latitude ), Double .parseDouble (longitude ));
9062 if (mOnGetLocationListener != null ) {
9163 mOnGetLocationListener .getLocation (lastLatitude , lastLongitude , latitude , longitude , country , locality , street );
9264 }
93- // }
94- // }).start();
9565 }
9666
9767 @ Override
@@ -100,6 +70,21 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
10070 }
10171 };
10272
73+ @ Override
74+ public void onCreate () {
75+ super .onCreate ();
76+ locationUtils = new LocationUtils (App .getInstance ());
77+ new Thread (new Runnable () {
78+ @ Override
79+ public void run () {
80+ Looper .prepare ();
81+ isSuccess = locationUtils .init (1000 , 0 , mOnLocationChangeListener );
82+ if (isSuccess ) ToastUtils .showShortToastSafe (App .getInstance (), "init success" );
83+ Looper .loop ();
84+ }
85+ }).start ();
86+ }
87+
10388 @ Nullable
10489 @ Override
10590 public IBinder onBind (Intent intent ) {
@@ -114,8 +99,10 @@ public LocationService getService() {
11499
115100 @ Override
116101 public void onDestroy () {
117- super .onDestroy ();
118102 locationUtils .removeListener ();
103+ // 一定要制空,否则内存泄漏
104+ mOnGetLocationListener = null ;
105+ super .onDestroy ();
119106 }
120107
121108 /**
0 commit comments