Skip to content

Commit 309c2f0

Browse files
committed
see 11/23 log
1 parent bed457e commit 309c2f0

File tree

7 files changed

+41
-45
lines changed

7 files changed

+41
-45
lines changed

README-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ getEntries : 获取压缩文件中的文件对象
502502
***
503503
Gradle:
504504
``` groovy
505-
compile 'com.blankj:utilcode:1.3.3'
505+
compile 'com.blankj:utilcode:1.3.4'
506506
```
507507

508508
### Proguard

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ getEntries
502502
***
503503
Gradle:
504504
``` groovy
505-
compile 'com.blankj:utilcode:1.3.3'
505+
compile 'com.blankj:utilcode:1.3.4'
506506
```
507507

508508
### Proguard

app/src/main/java/com/blankj/androidutilcode/Services/LocationService.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.blankj.androidutilcode.App;
1313
import com.blankj.utilcode.utils.LocationUtils;
14+
import com.blankj.utilcode.utils.ThreadPoolUtils;
1415
import com.blankj.utilcode.utils.ToastUtils;
1516

1617
/**
@@ -23,8 +24,8 @@
2324
*/
2425
public 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
/**

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ protected void onCreate(Bundle savedInstanceState) {
3232

3333
tvAboutLocation = (TextView) findViewById(R.id.tv_about_location);
3434

35+
tvAboutLocation.setText("lastLatitude: unknown" +
36+
"\nlastLongitude: unknown" +
37+
"\nlatitude: unknown" +
38+
"\nlongitude: unknown" +
39+
"\ngetCountryName: unknown" +
40+
"\ngetLocality: unknown" +
41+
"\ngetStreet: unknown"
42+
);
43+
3544
bindService(new Intent(this, LocationService.class), conn, Context.BIND_AUTO_CREATE);
3645
}
3746

3847
@Override
3948
protected void onDestroy() {
40-
super.onDestroy();
4149
unbindService(conn);
50+
super.onDestroy();
4251
}
4352

4453
ServiceConnection conn = new ServiceConnection() {

update_log.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
###
2+
#### 16/11/23 LocationUtils测试完毕,发布1.3.4
3+
#### 16/11/22 查看LocationActivity内存泄漏
24
#### 16/11/21 优化README
35
#### 16/11/20 完善LocationUtils
46
#### 16/11/19 完善SizeUtils

utilcode/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
defaultConfig {
88
minSdkVersion 11
99
targetSdkVersion 24
10-
versionCode 12
11-
versionName "1.3.3"
10+
versionCode 13
11+
versionName "1.3.4"
1212
}
1313
buildTypes {
1414
release {
@@ -25,5 +25,5 @@ dependencies {
2525
provided 'com.android.support:support-v4:24.0.0'
2626
provided 'com.android.support:design:24.0.0'
2727
}
28-
//apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
28+
apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
2929
//gradlew bintrayUpload

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public boolean isGpsEnabled() {
5454
* 打开Gps设置界面
5555
*/
5656
public void openGpsSettings() {
57-
mContext.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
57+
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
58+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
59+
mContext.startActivity(intent);
5860
}
5961

6062
/**
@@ -76,10 +78,6 @@ public boolean init(long minTime, long minDistance, OnLocationChangeListener lis
7678
if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
7779
&& !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
7880
ToastUtils.showShortToastSafe(mContext, "无法定位,请打开定位服务");
79-
openGpsSettings();
80-
}
81-
if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
82-
&& !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
8381
return false;
8482
}
8583
String provider = mLocationManager.getBestProvider(getCriteria(), true);

0 commit comments

Comments
 (0)