Skip to content

Commit 8338c25

Browse files
committed
see 11/17 log
1 parent d732e54 commit 8338c25

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

README-CN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ toggleSoftInput : 切换键盘显示与否状态
253253

254254
> - **定位相关→[LocationUtils.java][location.java]**
255255
```
256-
256+
LocationUtils : LocationUtils构造函数
257+
isGpsEnabled : 判断Gps是否可用
258+
openGpsSettings : 打开Gps设置界面
259+
init : 初始化
260+
getAddress : 根据经纬度获取地理位置
261+
getCountryName : 根据经纬度获取所在国家
262+
getLocality : 根据经纬度获取所在地
263+
getStreet : 根据经纬度获取所在街道
264+
removeAndGc : 移除并gc
257265
```
258266

259267
> - **日志相关→[LogUtils.java][log.java][Test][log.test]**
@@ -563,6 +571,8 @@ limitations under the License.
563571

564572
[keyboard.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/KeyboardUtils.java
565573

574+
[location.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
575+
566576
[log.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/LogUtils.java
567577
[log.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/LogUtilsTest.java
568578

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ showSoftInput
251251
toggleSoftInput
252252
```
253253

254+
> - **About Location→[LocationUtils.java][location.java]**
255+
```
256+
LocationUtils
257+
isGpsEnabled
258+
openGpsSettings
259+
init
260+
getAddress
261+
getCountryName
262+
getLocality
263+
getStreet
264+
removeAndGc
265+
```
266+
254267
> - **About Log→[LogUtils.java][log.java][Test][log.test]**
255268
```
256269
init
@@ -558,6 +571,8 @@ limitations under the License.
558571

559572
[keyboard.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/KeyboardUtils.java
560573

574+
[location.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
575+
561576
[log.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/LogUtils.java
562577
[log.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/LogUtilsTest.java
563578

app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
*/
2020
public class LocationActivity extends Activity {
2121

22-
Context mContext;
23-
TextView tvAboutLocation;
22+
Context mContext;
23+
TextView tvAboutLocation;
24+
LocationUtils locationUtils;
2425

2526
@Override
2627
protected void onCreate(Bundle savedInstanceState) {
@@ -30,16 +31,15 @@ protected void onCreate(Bundle savedInstanceState) {
3031
tvAboutLocation = (TextView) findViewById(R.id.tv_about_location);
3132
mContext = this;
3233

33-
LocationUtils.getInstance(mContext).init(1000, 0, new LocationUtils.OnLocationChangeListener() {
34+
locationUtils = new LocationUtils(this);
35+
locationUtils.init(100, 0, new LocationUtils.OnLocationChangeListener() {
3436
@Override
3537
public void onLocationChanged(Location location) {
36-
//得到纬度
3738
double latitude = location.getLatitude();
38-
//得到经度
3939
double longitude = location.getLongitude();
40-
tvAboutLocation.setText("getCountryName:" + LocationUtils.getInstance(mContext).getCountryName(latitude, longitude) +
41-
"\ngetLocality:" + LocationUtils.getInstance(mContext).getLocality(latitude, longitude) +
42-
"\ngetStreet:" + LocationUtils.getInstance(mContext).getStreet(latitude, longitude)
40+
tvAboutLocation.setText("getCountryName:" + locationUtils.getCountryName(latitude, longitude) +
41+
"\ngetLocality:" + locationUtils.getLocality(latitude, longitude) +
42+
"\ngetStreet:" + locationUtils.getStreet(latitude, longitude)
4343
);
4444
}
4545

utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,24 @@
2828
*/
2929
public class LocationUtils {
3030

31-
private volatile static LocationUtils uniqueInstance;
32-
31+
private Context mContext;
3332
private OnLocationChangeListener mListener;
34-
3533
private MyLocationListener myLocationListener;
36-
3734
private LocationManager mLocationManager;
3835

39-
private Context mContext;
40-
4136
/**
4237
* LocationUtils构造函数
4338
*
4439
* @param context 上下文
4540
*/
46-
private LocationUtils(Context context) {
41+
public LocationUtils(Context context) {
4742
mContext = context;
4843
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
4944
myLocationListener = new MyLocationListener();
5045
}
5146

5247
/**
53-
* 获取单例
54-
*
55-
* @param context 上下文
56-
* @return LocationUtils对象
57-
*/
58-
public static LocationUtils getInstance(Context context) {
59-
if (uniqueInstance == null) {
60-
synchronized (LocationUtils.class) {
61-
if (uniqueInstance == null) {
62-
uniqueInstance = new LocationUtils(context);
63-
}
64-
}
65-
}
66-
return uniqueInstance;
67-
}
68-
69-
/**
70-
* 判断Gps是否打开
48+
* 判断Gps是否可用
7149
*
7250
* @return {@code true}: 是<br>{@code false}: 否
7351
*/
@@ -84,7 +62,7 @@ public void openGpsSettings() {
8462

8563
/**
8664
* 初始化
87-
* <p>使用完记得调用{@link #remove()}</p>
65+
* <p>使用完记得调用{@link #removeAndGc()}</p>
8866
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
8967
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
9068
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
@@ -136,7 +114,7 @@ public Address getAddress(double latitude, double longitude) {
136114
}
137115

138116
/**
139-
* 根据经纬度获取国家的名字
117+
* 根据经纬度获取所在国家
140118
*
141119
* @param latitude 纬度
142120
* @param longitude 经度
@@ -160,7 +138,7 @@ public String getLocality(double latitude, double longitude) {
160138
}
161139

162140
/**
163-
* 根据经纬度获取街道名称
141+
* 根据经纬度获取所在街道
164142
*
165143
* @param latitude 纬度
166144
* @param longitude 经度
@@ -171,6 +149,18 @@ public String getStreet(double latitude, double longitude) {
171149
return address == null ? null : address.getAddressLine(0);
172150
}
173151

152+
/**
153+
* 移除并gc
154+
*/
155+
public void removeAndGc() {
156+
if (mLocationManager != null) {
157+
mLocationManager.removeUpdates(myLocationListener);
158+
mLocationManager = null;
159+
myLocationListener = null;
160+
System.gc();
161+
}
162+
}
163+
174164
private class MyLocationListener
175165
implements LocationListener {
176166
/**
@@ -225,19 +215,6 @@ public void onProviderDisabled(String provider) {
225215
}
226216
}
227217

228-
/**
229-
* 移除
230-
* <p>销毁时一定要移除,否则会内存泄漏</p>
231-
*/
232-
public void remove() {
233-
if (mLocationManager != null) {
234-
mLocationManager.removeUpdates(myLocationListener);
235-
mLocationManager = null;
236-
myLocationListener = null;
237-
System.gc();
238-
}
239-
}
240-
241218
public interface OnLocationChangeListener {
242219

243220
/**

0 commit comments

Comments
 (0)