see 11/22 log · self-java/AndroidUtilCode@bed457e · GitHub
Skip to content

Commit bed457e

Browse files
author
cmj
committed
see 11/22 log
1 parent 9d6e759 commit bed457e

7 files changed

Lines changed: 211 additions & 72 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.blankj.androidutilcode.services;
2+
3+
import android.app.Service;
4+
import android.content.Intent;
5+
import android.location.Location;
6+
import android.os.Binder;
7+
import android.os.Bundle;
8+
import android.os.IBinder;
9+
import android.os.Looper;
10+
import android.support.annotation.Nullable;
11+
12+
import com.blankj.androidutilcode.App;
13+
import com.blankj.utilcode.utils.LocationUtils;
14+
import com.blankj.utilcode.utils.ToastUtils;
15+
16+
/**
17+
* <pre>
18+
* author: Blankj
19+
* blog : http://blankj.com
20+
* time : 2016/11/21
21+
* desc : Location服务
22+
* </pre>
23+
*/
24+
public class LocationService extends Service {
25+
26+
private boolean isSuccess;
27+
private LocationUtils locationUtils;
28+
private String lastLatitude = "loading...";
29+
private String lastLongitude = "loading...";
30+
private String latitude = "loading...";
31+
private String longitude = "loading...";
32+
private String country = "loading...";
33+
private String locality = "loading...";
34+
private String street = "loading...";
35+
private OnGetLocationListener mOnGetLocationListener;
36+
private Thread mThread;
37+
38+
public void setOnGetLocationListener(OnGetLocationListener onGetLocationListener) {
39+
mOnGetLocationListener = onGetLocationListener;
40+
}
41+
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+
66+
private LocationUtils.OnLocationChangeListener mOnLocationChangeListener = new LocationUtils.OnLocationChangeListener() {
67+
@Override
68+
public void getLastKnownLocation(Location location) {
69+
lastLatitude = String.valueOf(location.getLatitude());
70+
lastLongitude = String.valueOf(location.getLongitude());
71+
if (mOnGetLocationListener != null) {
72+
mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
73+
}
74+
}
75+
76+
@Override
77+
public void onLocationChanged(final Location location) {
78+
latitude = String.valueOf(location.getLatitude());
79+
longitude = String.valueOf(location.getLongitude());
80+
if (mOnGetLocationListener != null) {
81+
mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
82+
}
83+
// 开启新线程来获取地理位置
84+
// new Thread(new Runnable() {
85+
// @Override
86+
// public void run() {
87+
country = locationUtils.getCountryName(Double.parseDouble(latitude), Double.parseDouble(longitude));
88+
locality = locationUtils.getLocality(Double.parseDouble(latitude), Double.parseDouble(longitude));
89+
street = locationUtils.getStreet(Double.parseDouble(latitude), Double.parseDouble(longitude));
90+
if (mOnGetLocationListener != null) {
91+
mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
92+
}
93+
// }
94+
// }).start();
95+
}
96+
97+
@Override
98+
public void onStatusChanged(String provider, int status, Bundle extras) {
99+
100+
}
101+
};
102+
103+
@Nullable
104+
@Override
105+
public IBinder onBind(Intent intent) {
106+
return new LocationBinder();
107+
}
108+
109+
public class LocationBinder extends Binder {
110+
public LocationService getService() {
111+
return LocationService.this;
112+
}
113+
}
114+
115+
@Override
116+
public void onDestroy() {
117+
super.onDestroy();
118+
locationUtils.removeListener();
119+
}
120+
121+
/**
122+
* 获取位置监听器
123+
*/
124+
public interface OnGetLocationListener {
125+
void getLocation(
126+
String lastLatitude, String lastLongitude,
127+
String latitude, String longitude,
128+
String country, String locality, String street
129+
);
130+
}
131+
}
Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.blankj.androidutilcode.activities;
22

33
import android.app.Activity;
4+
import android.content.ComponentName;
45
import android.content.Context;
5-
import android.location.Location;
6+
import android.content.Intent;
7+
import android.content.ServiceConnection;
68
import android.os.Bundle;
7-
import android.os.Looper;
8-
import android.os.Message;
9+
import android.os.IBinder;
910
import android.widget.TextView;
1011

11-
import com.blankj.androidutilcode.App;
1212
import com.blankj.androidutilcode.R;
13-
import com.blankj.utilcode.utils.HandlerUtils;
14-
import com.blankj.utilcode.utils.LocationUtils;
15-
import com.blankj.utilcode.utils.LogUtils;
13+
import com.blankj.androidutilcode.services.LocationService;
1614

1715
/**
1816
* <pre>
@@ -24,9 +22,8 @@
2422
*/
2523
public class LocationActivity extends Activity {
2624

27-
private TextView tvAboutLocation;
28-
private LocationUtils locationUtils;
29-
private double latitude, longitude;
25+
private TextView tvAboutLocation;
26+
private LocationService mLocationService;
3027

3128
@Override
3229
protected void onCreate(Bundle savedInstanceState) {
@@ -35,31 +32,42 @@ protected void onCreate(Bundle savedInstanceState) {
3532

3633
tvAboutLocation = (TextView) findViewById(R.id.tv_about_location);
3734

38-
locationUtils = new LocationUtils(App.getInstance());
39-
//注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);
40-
// 然后执行handler.sendMessage(),更新位置
41-
locationUtils.init(1000, 0, new LocationUtils.OnLocationChangeListener() {
42-
@Override
43-
public void onLocationChanged(Location location) {
44-
latitude = location.getLatitude();
45-
longitude = location.getLongitude();
46-
tvAboutLocation.setText("latitude: " + latitude +
47-
"\nlongitude: " + longitude +
48-
"\ngetCountryName: " + locationUtils.getCountryName(latitude, longitude) +
49-
"\ngetLocality: " + locationUtils.getLocality(latitude, longitude) +
50-
"\ngetStreet: " + locationUtils.getStreet(latitude, longitude)
51-
);
52-
}
53-
@Override
54-
public void onStatusChanged(String provider, int status, Bundle extras) {
55-
56-
}
57-
});
35+
bindService(new Intent(this, LocationService.class), conn, Context.BIND_AUTO_CREATE);
5836
}
5937

6038
@Override
6139
protected void onDestroy() {
62-
locationUtils.removeAndGc();
6340
super.onDestroy();
41+
unbindService(conn);
6442
}
43+
44+
ServiceConnection conn = new ServiceConnection() {
45+
@Override
46+
public void onServiceDisconnected(ComponentName name) {
47+
48+
}
49+
50+
@Override
51+
public void onServiceConnected(ComponentName name, IBinder service) {
52+
mLocationService = ((LocationService.LocationBinder) service).getService();
53+
mLocationService.setOnGetLocationListener(new LocationService.OnGetLocationListener() {
54+
@Override
55+
public void getLocation(final String lastLatitude, final String lastLongitude, final String latitude, final String longitude, final String country, final String locality, final String street) {
56+
runOnUiThread(new Runnable() {
57+
@Override
58+
public void run() {
59+
tvAboutLocation.setText("lastLatitude: " + lastLatitude +
60+
"\nlastLongitude: " + lastLongitude +
61+
"\nlatitude: " + latitude +
62+
"\nlongitude: " + longitude +
63+
"\ngetCountryName: " + country +
64+
"\ngetLocality: " + locality +
65+
"\ngetStreet: " + street
66+
);
67+
}
68+
});
69+
}
70+
});
71+
}
72+
};
6573
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void setAboutNetwork() {
6363
"\nisWifiConnected: " + NetworkUtils.isWifiConnected(mContext) +
6464
"\nisWifiAvailable: " + NetworkUtils.isWifiAvailable(mContext) +
6565
"\ngetNetworkOperatorName: " + NetworkUtils.getNetworkOperatorName(mContext) +
66-
"\ngetNetworkTypeName: " + NetworkUtils.getNetworkTypeName(mContext) +
66+
"\ngetNetworkTypeName: " + NetworkUtils.getNetworkType(mContext) +
6767
"\ngetIPAddress: " + NetworkUtils.getIPAddress(true) +
6868
"\ngetDomainAddress: " + NetworkUtils.getDomainAddress("baidu.com")
6969
);

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

Lines changed: 19 additions & 28 deletions

0 commit comments

Comments
 (0)