Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
2c8ed3e7
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2c8ed3e7
编写于
11月 23, 2016
作者:
C
cmj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 11/23 log
上级
b6adc67c
变更
18
隐藏空白更改
内联
并排
Showing
18 changed file
with
90 addition
and
82 deletion
+90
-82
README-CN.md
README-CN.md
+9
-9
README.md
README.md
+3
-3
app/src/main/java/com/blankj/androidutilcode/Services/LocationService.java
.../com/blankj/androidutilcode/Services/LocationService.java
+6
-8
app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java
...m/blankj/androidutilcode/activities/ActivityActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/AppActivity.java
...va/com/blankj/androidutilcode/activities/AppActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/CleanActivity.java
.../com/blankj/androidutilcode/activities/CleanActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java
...com/blankj/androidutilcode/activities/DeviceActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java
...om/blankj/androidutilcode/activities/HandlerActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java
.../com/blankj/androidutilcode/activities/ImageActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java
...m/blankj/androidutilcode/activities/KeyboardActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java
...m/blankj/androidutilcode/activities/LocationActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java
...om/blankj/androidutilcode/activities/NetworkActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java
.../com/blankj/androidutilcode/activities/PhoneActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java
...om/blankj/androidutilcode/activities/ProcessActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java
...com/blankj/androidutilcode/activities/SDCardActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/SnackbarActivity.java
...m/blankj/androidutilcode/activities/SnackbarActivity.java
+1
-1
app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java
.../com/blankj/androidutilcode/activities/ToastActivity.java
+1
-1
utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
...rc/main/java/com/blankj/utilcode/utils/LocationUtils.java
+58
-48
未找到文件。
README-CN.md
浏览文件 @
2c8ed3e7
...
...
@@ -258,15 +258,15 @@ toggleSoftInput : 切换键盘显示与否状态
> - **定位相关→[LocationUtils.java][location.java]→[Demo][location.demo]**
```
LocationUtils : LocationUtils构造函数
is
GpsEnabled : 判断Gps
是否可用
openGpsSettings : 打开Gps设置界面
init : 初始化
getAddress : 根据经纬度获取地理位置
get
CountryName : 根据经纬度获取所在国家
get
Locality : 根据经纬度获取所在地
get
Street : 根据经纬度获取所在街道
removeAndGc : 移除并gc
isGpsEnabled : 判断Gps是否可用
is
LocationEnabled : 判断定位
是否可用
openGpsSettings
: 打开Gps设置界面
register : 注册
unregister : 注销
get
Address : 根据经纬度获取地理位置
get
CountryName : 根据经纬度获取所在国家
get
Locality : 根据经纬度获取所在地
getStreet : 根据经纬度获取所在街道
```
> - **日志相关→[LogUtils.java][log.java]→[Test][log.test]**
...
...
README.md
浏览文件 @
2c8ed3e7
...
...
@@ -258,15 +258,15 @@ toggleSoftInput
> - **About Location→[LocationUtils.java][location.java]→[Demo][location.demo]**
```
LocationUtils
isGpsEnabled
isLocationEnabled
openGpsSettings
init
register
unregister
getAddress
getCountryName
getLocality
getStreet
removeAndGc
```
> - **About Log→[LogUtils.java][log.java]→[Test][log.test]**
...
...
app/src/main/java/com/blankj/androidutilcode/Services/LocationService.java
浏览文件 @
2c8ed3e7
...
...
@@ -24,8 +24,7 @@ import com.blankj.utilcode.utils.ToastUtils;
*/
public
class
LocationService
extends
Service
{
private
boolean
isSuccess
;
private
LocationUtils
locationUtils
;
private
boolean
isSuccess
;
private
String
lastLatitude
=
"loading..."
;
private
String
lastLongitude
=
"loading..."
;
private
String
latitude
=
"loading..."
;
...
...
@@ -56,9 +55,9 @@ public class LocationService extends Service {
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
lastLatitude
,
lastLongitude
,
latitude
,
longitude
,
country
,
locality
,
street
);
}
country
=
locationUtils
.
getCountryName
(
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
locality
=
locationUtils
.
getLocality
(
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
street
=
locationUtils
.
getStreet
(
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
country
=
LocationUtils
.
getCountryName
(
App
.
getInstance
(),
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
locality
=
LocationUtils
.
getLocality
(
App
.
getInstance
(),
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
street
=
LocationUtils
.
getStreet
(
App
.
getInstance
(),
Double
.
parseDouble
(
latitude
),
Double
.
parseDouble
(
longitude
));
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
lastLatitude
,
lastLongitude
,
latitude
,
longitude
,
country
,
locality
,
street
);
}
...
...
@@ -73,12 +72,11 @@ public class LocationService extends Service {
@Override
public
void
onCreate
()
{
super
.
onCreate
();
locationUtils
=
new
LocationUtils
(
App
.
getInstance
());
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
Looper
.
prepare
();
isSuccess
=
locationUtils
.
init
(
100
0
,
0
,
mOnLocationChangeListener
);
isSuccess
=
LocationUtils
.
register
(
App
.
getInstance
(),
0
,
0
,
mOnLocationChangeListener
);
if
(
isSuccess
)
ToastUtils
.
showShortToastSafe
(
App
.
getInstance
(),
"init success"
);
Looper
.
loop
();
}
...
...
@@ -99,7 +97,7 @@ public class LocationService extends Service {
@Override
public
void
onDestroy
()
{
locationUtils
.
removeListen
er
();
LocationUtils
.
unregist
er
();
// 一定要制空,否则内存泄漏
mOnGetLocationListener
=
null
;
super
.
onDestroy
();
...
...
app/src/main/java/com/blankj/androidutilcode/activities/ActivityActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -13,7 +13,7 @@ import com.blankj.utilcode.utils.ActivityUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : Activity工具类
测试
* desc : Activity工具类
Demo
* </pre>
*/
...
...
app/src/main/java/com/blankj/androidutilcode/activities/AppActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -14,7 +14,7 @@ import com.blankj.utilcode.utils.AppUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : App工具类
测试
* desc : App工具类
Demo
* </pre>
*/
...
...
app/src/main/java/com/blankj/androidutilcode/activities/CleanActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -16,7 +16,7 @@ import java.io.File;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/29
* desc : Clean工具类
测试
* desc : Clean工具类
Demo
* </pre>
*/
public
class
CleanActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/DeviceActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -14,7 +14,7 @@ import com.blankj.utilcode.utils.DeviceUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/27
* desc : Device工具类
测试
* desc : Device工具类
Demo
* </pre>
*/
public
class
DeviceActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -16,7 +16,7 @@ import com.blankj.utilcode.utils.LogUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/27
* desc : Handler工具类
测试
* desc : Handler工具类
Demo
* </pre>
*/
public
class
HandlerActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -16,7 +16,7 @@ import com.blankj.utilcode.utils.SizeUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/26
* desc : Image工具类
测试
* desc : Image工具类
Demo
* </pre>
*/
public
class
ImageActivity
extends
Activity
{
...
...
app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -17,7 +17,7 @@ import com.blankj.utilcode.utils.KeyboardUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/27
* desc : Keyboard工具类
测试
* desc : Keyboard工具类
Demo
* </pre>
*/
public
class
KeyboardActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -17,7 +17,7 @@ import com.blankj.androidutilcode.services.LocationService;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : Location工具类
测试
* desc : Location工具类
Demo
* </pre>
*/
public
class
LocationActivity
extends
Activity
{
...
...
app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -15,7 +15,7 @@ import com.blankj.utilcode.utils.NetworkUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : Network工具类
测试
* desc : Network工具类
Demo
* </pre>
*/
public
class
NetworkActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -13,7 +13,7 @@ import com.blankj.utilcode.utils.PhoneUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : Phone工具类
测试
* desc : Phone工具类
Demo
* </pre>
*/
public
class
PhoneActivity
extends
Activity
implements
...
...
app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -13,7 +13,7 @@ import com.blankj.utilcode.utils.ToastUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/13
* desc : Process工具类
测试
* desc : Process工具类
Demo
* </pre>
*/
public
class
ProcessActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -12,7 +12,7 @@ import com.blankj.utilcode.utils.SDCardUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/27
* desc : SDCard工具类
测试
* desc : SDCard工具类
Demo
* </pre>
*/
public
class
SDCardActivity
extends
Activity
{
...
...
app/src/main/java/com/blankj/androidutilcode/activities/SnackbarActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -15,7 +15,7 @@ import com.blankj.utilcode.utils.ToastUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/10/17
* desc : Snackbar工具类
测试
* desc : Snackbar工具类
Demo
* </pre>
*/
public
class
SnackbarActivity
extends
Activity
...
...
app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java
浏览文件 @
2c8ed3e7
...
...
@@ -15,7 +15,7 @@ import com.blankj.utilcode.utils.ToastUtils;
* author: Blankj
* blog : http://blankj.com
* time : 2016/9/29
* desc : Toast工具类
测试
* desc : Toast工具类
Demo
* </pre>
*/
public
class
ToastActivity
extends
Activity
...
...
utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
浏览文件 @
2c8ed3e7
...
...
@@ -26,62 +26,67 @@ import java.util.Locale;
*/
public
class
LocationUtils
{
private
Context
mContext
;
private
OnLocationChangeListener
mListener
;
private
MyLocationListener
myLocationListener
;
private
LocationManager
mLocationManager
;
private
static
OnLocationChangeListener
mListener
;
private
static
MyLocationListener
myLocationListener
;
private
static
LocationManager
mLocationManager
;
public
LocationUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
/**
*
LocationUtils构造函数
*
判断Gps是否可用
*
* @
param context 上下文
* @
return {@code true}: 是<br>{@code false}: 否
*/
public
LocationUtils
(
Context
context
)
{
mContext
=
context
;
mLocationManager
=
(
LocationManager
)
context
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
public
static
boolean
isGpsEnabled
(
Context
context
)
{
LocationManager
lm
=
(
LocationManager
)
context
.
getSystemService
(
Context
.
LOCATION_SERVICE
)
;
return
lm
.
isProviderEnabled
(
LocationManager
.
GPS_PROVIDER
);
}
/**
* 判断
Gps
是否可用
* 判断
定位
是否可用
*
* @return {@code true}: 是<br>{@code false}: 否
*/
public
boolean
isGpsEnabled
()
{
return
mLocationManager
.
isProviderEnabled
(
LocationManager
.
GPS_PROVIDER
);
public
static
boolean
isLocationEnabled
(
Context
context
)
{
LocationManager
lm
=
(
LocationManager
)
context
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
return
lm
.
isProviderEnabled
(
LocationManager
.
NETWORK_PROVIDER
)
||
lm
.
isProviderEnabled
(
LocationManager
.
GPS_PROVIDER
);
}
/**
* 打开Gps设置界面
*/
public
void
openGpsSettings
(
)
{
public
static
void
openGpsSettings
(
Context
context
)
{
Intent
intent
=
new
Intent
(
Settings
.
ACTION_LOCATION_SOURCE_SETTINGS
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
mC
ontext
.
startActivity
(
intent
);
c
ontext
.
startActivity
(
intent
);
}
/**
*
初始化
* <p>使用完记得调用{@link #
removeListen
er()}</p>
*
注册
* <p>使用完记得调用{@link #
unregist
er()}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
* <p>如果{@code minDistance}不为0,则以{@code minDistance}为准;{@code minDistance}为0,则通过{@code minTime}来定时更新;两者都为0,则随时刷新</p>
* <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p>
* <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p>
* <p>两者都为0,则随时刷新。</p>
*
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public
boolean
init
(
long
minTime
,
long
minDistance
,
OnLocationChangeListener
listener
)
{
public
static
boolean
register
(
Context
context
,
long
minTime
,
long
minDistance
,
OnLocationChangeListener
listener
)
{
if
(
listener
==
null
)
return
false
;
mLocationManager
=
(
LocationManager
)
context
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
mListener
=
listener
;
if
(!
mLocationManager
.
isProviderEnabled
(
LocationManager
.
NETWORK_PROVIDER
)
&&
!
mLocationManager
.
isProviderEnabled
(
LocationManager
.
GPS_PROVIDER
))
{
ToastUtils
.
showShortToastSafe
(
mContext
,
"无法定位,请打开定位服务"
);
if
(!
isLocationEnabled
(
context
))
{
ToastUtils
.
showShortToastSafe
(
context
,
"无法定位,请打开定位服务"
);
return
false
;
}
String
provider
=
mLocationManager
.
getBestProvider
(
getCriteria
(),
true
);
if
(
provider
==
null
)
return
false
;
Location
location
=
mLocationManager
.
getLastKnownLocation
(
provider
);
if
(
location
!=
null
)
listener
.
getLastKnownLocation
(
location
);
if
(
myLocationListener
==
null
)
myLocationListener
=
new
MyLocationListener
();
...
...
@@ -89,12 +94,26 @@ public class LocationUtils {
return
true
;
}
/**
* 注销
*/
public
static
void
unregister
()
{
if
(
mLocationManager
!=
null
)
{
if
(
myLocationListener
!=
null
)
{
mLocationManager
.
removeUpdates
(
myLocationListener
);
myLocationListener
=
null
;
}
mLocationManager
=
null
;
}
}
/**
* 设置定位参数
*
* @return {@link Criteria}
*/
private
Criteria
getCriteria
()
{
private
static
Criteria
getCriteria
()
{
Criteria
criteria
=
new
Criteria
();
//设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria
.
setAccuracy
(
Criteria
.
ACCURACY_FINE
);
...
...
@@ -114,12 +133,13 @@ public class LocationUtils {
/**
* 根据经纬度获取地理位置
*
* @param context 上下文
* @param latitude 纬度
* @param longitude 经度
* @return {@link Address}
*/
public
Address
getAddress
(
double
latitude
,
double
longitude
)
{
Geocoder
geocoder
=
new
Geocoder
(
mC
ontext
,
Locale
.
getDefault
());
public
static
Address
getAddress
(
Context
context
,
double
latitude
,
double
longitude
)
{
Geocoder
geocoder
=
new
Geocoder
(
c
ontext
,
Locale
.
getDefault
());
try
{
List
<
Address
>
addresses
=
geocoder
.
getFromLocation
(
latitude
,
longitude
,
1
);
if
(
addresses
.
size
()
>
0
)
return
addresses
.
get
(
0
);
...
...
@@ -132,53 +152,43 @@ public class LocationUtils {
/**
* 根据经纬度获取所在国家
*
* @param context 上下文
* @param latitude 纬度
* @param longitude 经度
* @return
{@link Address}
* @return
所在国家
*/
public
String
getCountryName
(
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
latitude
,
longitude
);
public
static
String
getCountryName
(
Context
context
,
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
context
,
latitude
,
longitude
);
return
address
==
null
?
"unknown"
:
address
.
getCountryName
();
}
/**
* 根据经纬度获取所在地
*
* @param context 上下文
* @param latitude 纬度
* @param longitude 经度
* @return
{@link Address}
* @return
所在地
*/
public
String
getLocality
(
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
latitude
,
longitude
);
public
static
String
getLocality
(
Context
context
,
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
context
,
latitude
,
longitude
);
return
address
==
null
?
"unknown"
:
address
.
getLocality
();
}
/**
* 根据经纬度获取所在街道
*
* @param context 上下文
* @param latitude 纬度
* @param longitude 经度
* @return
{@link Address}
* @return
所在街道
*/
public
String
getStreet
(
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
latitude
,
longitude
);
public
static
String
getStreet
(
Context
context
,
double
latitude
,
double
longitude
)
{
Address
address
=
getAddress
(
context
,
latitude
,
longitude
);
return
address
==
null
?
"unknown"
:
address
.
getAddressLine
(
0
);
}
/**
* 移除监听
*/
public
void
removeListener
()
{
if
(
mLocationManager
!=
null
)
{
if
(
myLocationListener
!=
null
)
{
mLocationManager
.
removeUpdates
(
myLocationListener
);
myLocationListener
=
null
;
}
mLocationManager
=
null
;
}
}
private
class
MyLocationListener
private
static
class
MyLocationListener
implements
LocationListener
{
/**
* 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录