Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
bed457ee
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,体验更适合开发者的 AI 搜索 >>
提交
bed457ee
编写于
11月 22, 2016
作者:
C
cmj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 11/22 log
上级
9d6e7599
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
211 addition
and
72 deletion
+211
-72
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
app/src/main/java/com/blankj/androidutilcode/Services/LocationService.java
.../com/blankj/androidutilcode/Services/LocationService.java
+131
-0
app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java
...m/blankj/androidutilcode/activities/LocationActivity.java
+39
-31
app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java
...om/blankj/androidutilcode/activities/NetworkActivity.java
+1
-1
utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
...rc/main/java/com/blankj/utilcode/utils/LocationUtils.java
+19
-28
utilcode/src/main/java/com/blankj/utilcode/utils/LogUtils.java
...ode/src/main/java/com/blankj/utilcode/utils/LogUtils.java
+18
-12
utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java
...de/src/test/java/com/blankj/utilcode/utils/TestUtils.java
+2
-0
未找到文件。
app/src/main/AndroidManifest.xml
浏览文件 @
bed457ee
...
...
@@ -49,6 +49,7 @@
android:name=
".activities.KeyboardActivity"
android:windowSoftInputMode=
"stateHidden|adjustPan"
/>
<activity
android:name=
".activities.LocationActivity"
/>
<service
android:name=
".services.LocationService"
/>
<activity
android:name=
".activities.MainActivity"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
...
...
app/src/main/java/com/blankj/androidutilcode/Services/LocationService.java
0 → 100644
浏览文件 @
bed457ee
package
com.blankj.androidutilcode.services
;
import
android.app.Service
;
import
android.content.Intent
;
import
android.location.Location
;
import
android.os.Binder
;
import
android.os.Bundle
;
import
android.os.IBinder
;
import
android.os.Looper
;
import
android.support.annotation.Nullable
;
import
com.blankj.androidutilcode.App
;
import
com.blankj.utilcode.utils.LocationUtils
;
import
com.blankj.utilcode.utils.ToastUtils
;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/11/21
* desc : Location服务
* </pre>
*/
public
class
LocationService
extends
Service
{
private
boolean
isSuccess
;
private
LocationUtils
locationUtils
;
private
String
lastLatitude
=
"loading..."
;
private
String
lastLongitude
=
"loading..."
;
private
String
latitude
=
"loading..."
;
private
String
longitude
=
"loading..."
;
private
String
country
=
"loading..."
;
private
String
locality
=
"loading..."
;
private
String
street
=
"loading..."
;
private
OnGetLocationListener
mOnGetLocationListener
;
private
Thread
mThread
;
public
void
setOnGetLocationListener
(
OnGetLocationListener
onGetLocationListener
)
{
mOnGetLocationListener
=
onGetLocationListener
;
}
@Override
public
void
onCreate
()
{
super
.
onCreate
();
mThread
=
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
Looper
.
prepare
();
locationUtils
=
new
LocationUtils
(
App
.
getInstance
());
isSuccess
=
locationUtils
.
init
(
1000
,
0
,
mOnLocationChangeListener
);
if
(
isSuccess
)
{
ToastUtils
.
showShortToastSafe
(
App
.
getInstance
(),
"init success"
);
}
else
{
ToastUtils
.
showShortToastSafe
(
App
.
getInstance
(),
"init fail"
);
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
"unknown"
,
"unknown"
,
"unknown"
,
"unknown"
,
"unknown"
,
"unknown"
,
"unknown"
);
}
}
Looper
.
loop
();
}
});
mThread
.
start
();
}
private
LocationUtils
.
OnLocationChangeListener
mOnLocationChangeListener
=
new
LocationUtils
.
OnLocationChangeListener
()
{
@Override
public
void
getLastKnownLocation
(
Location
location
)
{
lastLatitude
=
String
.
valueOf
(
location
.
getLatitude
());
lastLongitude
=
String
.
valueOf
(
location
.
getLongitude
());
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
lastLatitude
,
lastLongitude
,
latitude
,
longitude
,
country
,
locality
,
street
);
}
}
@Override
public
void
onLocationChanged
(
final
Location
location
)
{
latitude
=
String
.
valueOf
(
location
.
getLatitude
());
longitude
=
String
.
valueOf
(
location
.
getLongitude
());
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
lastLatitude
,
lastLongitude
,
latitude
,
longitude
,
country
,
locality
,
street
);
}
// 开启新线程来获取地理位置
// new Thread(new Runnable() {
// @Override
// public void run() {
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
));
if
(
mOnGetLocationListener
!=
null
)
{
mOnGetLocationListener
.
getLocation
(
lastLatitude
,
lastLongitude
,
latitude
,
longitude
,
country
,
locality
,
street
);
}
// }
// }).start();
}
@Override
public
void
onStatusChanged
(
String
provider
,
int
status
,
Bundle
extras
)
{
}
};
@Nullable
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
new
LocationBinder
();
}
public
class
LocationBinder
extends
Binder
{
public
LocationService
getService
()
{
return
LocationService
.
this
;
}
}
@Override
public
void
onDestroy
()
{
super
.
onDestroy
();
locationUtils
.
removeListener
();
}
/**
* 获取位置监听器
*/
public
interface
OnGetLocationListener
{
void
getLocation
(
String
lastLatitude
,
String
lastLongitude
,
String
latitude
,
String
longitude
,
String
country
,
String
locality
,
String
street
);
}
}
app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java
浏览文件 @
bed457ee
package
com.blankj.androidutilcode.activities
;
import
android.app.Activity
;
import
android.content.ComponentName
;
import
android.content.Context
;
import
android.location.Location
;
import
android.content.Intent
;
import
android.content.ServiceConnection
;
import
android.os.Bundle
;
import
android.os.Looper
;
import
android.os.Message
;
import
android.os.IBinder
;
import
android.widget.TextView
;
import
com.blankj.androidutilcode.App
;
import
com.blankj.androidutilcode.R
;
import
com.blankj.utilcode.utils.HandlerUtils
;
import
com.blankj.utilcode.utils.LocationUtils
;
import
com.blankj.utilcode.utils.LogUtils
;
import
com.blankj.androidutilcode.services.LocationService
;
/**
* <pre>
...
...
@@ -24,9 +22,8 @@ import com.blankj.utilcode.utils.LogUtils;
*/
public
class
LocationActivity
extends
Activity
{
private
TextView
tvAboutLocation
;
private
LocationUtils
locationUtils
;
private
double
latitude
,
longitude
;
private
TextView
tvAboutLocation
;
private
LocationService
mLocationService
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -35,31 +32,42 @@ public class LocationActivity extends Activity {
tvAboutLocation
=
(
TextView
)
findViewById
(
R
.
id
.
tv_about_location
);
locationUtils
=
new
LocationUtils
(
App
.
getInstance
());
//注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);
// 然后执行handler.sendMessage(),更新位置
locationUtils
.
init
(
1000
,
0
,
new
LocationUtils
.
OnLocationChangeListener
()
{
@Override
public
void
onLocationChanged
(
Location
location
)
{
latitude
=
location
.
getLatitude
();
longitude
=
location
.
getLongitude
();
tvAboutLocation
.
setText
(
"latitude: "
+
latitude
+
"\nlongitude: "
+
longitude
+
"\ngetCountryName: "
+
locationUtils
.
getCountryName
(
latitude
,
longitude
)
+
"\ngetLocality: "
+
locationUtils
.
getLocality
(
latitude
,
longitude
)
+
"\ngetStreet: "
+
locationUtils
.
getStreet
(
latitude
,
longitude
)
);
}
@Override
public
void
onStatusChanged
(
String
provider
,
int
status
,
Bundle
extras
)
{
}
});
bindService
(
new
Intent
(
this
,
LocationService
.
class
),
conn
,
Context
.
BIND_AUTO_CREATE
);
}
@Override
protected
void
onDestroy
()
{
locationUtils
.
removeAndGc
();
super
.
onDestroy
();
unbindService
(
conn
);
}
ServiceConnection
conn
=
new
ServiceConnection
()
{
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
}
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
mLocationService
=
((
LocationService
.
LocationBinder
)
service
).
getService
();
mLocationService
.
setOnGetLocationListener
(
new
LocationService
.
OnGetLocationListener
()
{
@Override
public
void
getLocation
(
final
String
lastLatitude
,
final
String
lastLongitude
,
final
String
latitude
,
final
String
longitude
,
final
String
country
,
final
String
locality
,
final
String
street
)
{
runOnUiThread
(
new
Runnable
()
{
@Override
public
void
run
()
{
tvAboutLocation
.
setText
(
"lastLatitude: "
+
lastLatitude
+
"\nlastLongitude: "
+
lastLongitude
+
"\nlatitude: "
+
latitude
+
"\nlongitude: "
+
longitude
+
"\ngetCountryName: "
+
country
+
"\ngetLocality: "
+
locality
+
"\ngetStreet: "
+
street
);
}
});
}
});
}
};
}
app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java
浏览文件 @
bed457ee
...
...
@@ -63,7 +63,7 @@ public class NetworkActivity extends Activity
"\nisWifiConnected: "
+
NetworkUtils
.
isWifiConnected
(
mContext
)
+
"\nisWifiAvailable: "
+
NetworkUtils
.
isWifiAvailable
(
mContext
)
+
"\ngetNetworkOperatorName: "
+
NetworkUtils
.
getNetworkOperatorName
(
mContext
)
+
"\ngetNetworkTypeName: "
+
NetworkUtils
.
getNetworkType
Name
(
mContext
)
+
"\ngetNetworkTypeName: "
+
NetworkUtils
.
getNetworkType
(
mContext
)
+
"\ngetIPAddress: "
+
NetworkUtils
.
getIPAddress
(
true
)
+
"\ngetDomainAddress: "
+
NetworkUtils
.
getDomainAddress
(
"baidu.com"
)
);
...
...
utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java
浏览文件 @
bed457ee
...
...
@@ -5,24 +5,17 @@ import android.content.Intent;
import
android.location.Address
;
import
android.location.Criteria
;
import
android.location.Geocoder
;
import
android.location.GnssStatus
;
import
android.location.GpsSatellite
;
import
android.location.GpsStatus
;
import
android.location.Location
;
import
android.location.LocationListener
;
import
android.location.LocationManager
;
import
android.location.LocationProvider
;
import
android.os.Bundle
;
import
android.provider.Settings
;
import
android.util.Log
;
import
java.io.IOException
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Locale
;
import
static
android
.
support
.
v7
.
widget
.
StaggeredGridLayoutManager
.
TAG
;
/**
* <pre>
* author: Blankj
...
...
@@ -46,7 +39,6 @@ public class LocationUtils {
public
LocationUtils
(
Context
context
)
{
mContext
=
context
;
mLocationManager
=
(
LocationManager
)
context
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
myLocationListener
=
new
MyLocationListener
();
}
/**
...
...
@@ -67,19 +59,18 @@ public class LocationUtils {
/**
* 初始化
* <p>使用完记得调用{@link #remove
AndGc
()}</p>
* <p>使用完记得调用{@link #remove
Listener
()}</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>
参数1和2,如果参数2不为0,则以参数2为准;参数2为0,则通过时间来定时更新;两者
为0,则随时刷新</p>
* <p>
如果{@code minDistance}不为0,则以{@code minDistance}为准;{@code minDistance}为0,则通过{@code minTime}来定时更新;两者都
为0,则随时刷新</p>
*
* @param isUseOnce 是否只调用一次
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public
boolean
init
(
boolean
isUseOnce
,
long
minTime
,
long
minDistance
,
OnLocationChangeListener
listener
)
{
public
boolean
init
(
long
minTime
,
long
minDistance
,
OnLocationChangeListener
listener
)
{
if
(
listener
==
null
)
return
false
;
mListener
=
listener
;
if
(!
mLocationManager
.
isProviderEnabled
(
LocationManager
.
NETWORK_PROVIDER
)
...
...
@@ -92,18 +83,10 @@ public class LocationUtils {
return
false
;
}
String
provider
=
mLocationManager
.
getBestProvider
(
getCriteria
(),
true
);
Location
location
=
mLocationManager
.
getLastKnownLocation
(
provider
);
// 如果只调用一次
if
(
isUseOnce
)
{
if
(
location
!=
null
)
{
listener
.
onLocationChanged
(
location
);
return
true
;
}
else
{
return
false
;
}
}
if
(
provider
==
null
)
return
false
;
// 以下是实时调用
Location
location
=
mLocationManager
.
getLastKnownLocation
(
provider
);
if
(
location
!=
null
)
listener
.
getLastKnownLocation
(
location
);
if
(
myLocationListener
==
null
)
myLocationListener
=
new
MyLocationListener
();
mLocationManager
.
requestLocationUpdates
(
provider
,
minTime
,
minDistance
,
myLocationListener
);
return
true
;
}
...
...
@@ -185,14 +168,15 @@ public class LocationUtils {
}
/**
* 移除
并gc
* 移除
监听
*/
public
void
remove
AndGc
()
{
public
void
remove
Listener
()
{
if
(
mLocationManager
!=
null
)
{
mLocationManager
.
removeUpdates
(
myLocationListener
);
if
(
myLocationListener
!=
null
)
{
mLocationManager
.
removeUpdates
(
myLocationListener
);
myLocationListener
=
null
;
}
mLocationManager
=
null
;
myLocationListener
=
null
;
System
.
gc
();
}
}
...
...
@@ -252,6 +236,13 @@ public class LocationUtils {
public
interface
OnLocationChangeListener
{
/**
* 获取最后一次保留的坐标
*
* @param location 坐标
*/
void
getLastKnownLocation
(
Location
location
);
/**
* 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
*
...
...
utilcode/src/main/java/com/blankj/utilcode/utils/LogUtils.java
浏览文件 @
bed457ee
...
...
@@ -287,22 +287,28 @@ public class LogUtils {
* @param tag 标签
* @param content 内容
**/
private
synchronized
static
void
log2File
(
char
type
,
String
tag
,
String
content
)
{
private
synchronized
static
void
log2File
(
final
char
type
,
final
String
tag
,
final
String
content
)
{
if
(
content
==
null
)
return
;
Date
now
=
new
Date
();
String
date
=
new
SimpleDateFormat
(
"MM-dd"
,
Locale
.
getDefault
()).
format
(
now
);
String
fullPath
=
dir
+
date
+
".txt"
;
final
String
fullPath
=
dir
+
date
+
".txt"
;
if
(!
FileUtils
.
createOrExistsFile
(
fullPath
))
return
;
String
time
=
new
SimpleDateFormat
(
"MM-dd HH:mm:ss.SSS"
,
Locale
.
getDefault
()).
format
(
now
);
String
dateLogContent
=
time
+
":"
+
type
+
":"
+
tag
+
":"
+
content
+
'\n'
;
BufferedWriter
bw
=
null
;
try
{
bw
=
new
BufferedWriter
(
new
FileWriter
(
fullPath
,
true
));
bw
.
write
(
dateLogContent
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
CloseUtils
.
closeIO
(
bw
);
}
final
String
dateLogContent
=
time
+
":"
+
type
+
":"
+
tag
+
":"
+
content
+
'\n'
;
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
BufferedWriter
bw
=
null
;
try
{
bw
=
new
BufferedWriter
(
new
FileWriter
(
fullPath
,
true
));
bw
.
write
(
dateLogContent
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
CloseUtils
.
closeIO
(
bw
);
}
}
}).
start
();
}
}
utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java
浏览文件 @
bed457ee
...
...
@@ -5,6 +5,7 @@ import android.util.SparseArray;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.notification.Failure
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.RuntimeEnvironment
;
import
org.robolectric.annotation.Config
;
...
...
@@ -107,5 +108,6 @@ public class TestUtils {
@Test
public
void
test
()
throws
Exception
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录