Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
L
location share white
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
location share white
Commits
f53b130d
Commit
f53b130d
authored
Nov 26, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...移动位置获取代码到常驻服务
parent
501101fb
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
46 deletions
+59
-46
StayJobService.kt
...ava/com/base/locationsharewhite/service/StayJobService.kt
+49
-1
LocationMapActivity.kt
.../locationsharewhite/ui/locationmap/LocationMapActivity.kt
+5
-3
LocationPresenter.kt
...se/locationsharewhite/ui/locationmap/LocationPresenter.kt
+3
-24
MainPresenter.kt
...java/com/base/locationsharewhite/ui/main/MainPresenter.kt
+2
-18
No files found.
app/src/main/java/com/base/locationsharewhite/service/StayJobService.kt
View file @
f53b130d
...
...
@@ -10,6 +10,7 @@ import android.content.ComponentName
import
android.content.Context
import
android.content.Intent
import
android.content.pm.ServiceInfo
import
android.location.Location
import
android.os.Build
import
android.os.CountDownTimer
import
android.os.Looper
...
...
@@ -22,6 +23,13 @@ import com.google.android.gms.location.LocationCallback
import
com.google.android.gms.location.LocationRequest
import
com.google.android.gms.location.LocationResult
import
com.google.android.gms.location.LocationServices
import
com.google.android.gms.maps.model.LatLng
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.MainScope
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.isActive
import
kotlinx.coroutines.launch
import
kotlin.random.Random
...
...
@@ -40,8 +48,17 @@ class StayJobService : JobService() {
companion
object
{
val
ACTION_LOCATION_UPDATES
=
"ACTION_REQUEST_LOCATION_UPDATES"
private
val
NOTIFICATION_PERMANENT_ID
=
186
var
googleLocation
:
LatLng
?
=
null
//位置更新回调
private
var
locationCallback
:
LocationCallback
?
=
null
//位置的一次性回调
var
oneShotLocationCallBack
:
(()
->
Unit
)?
=
null
//更新频率
var
getGoogleServiceLocationDelay
:
Int
=
60
fun
Context
.
requestServiceLocationUpdates
()
{
startService
(
Intent
(
this
,
StayJobService
::
class
.
java
).
apply
{
action
=
ACTION_LOCATION_UPDATES
})
}
...
...
@@ -99,6 +116,9 @@ class StayJobService : JobService() {
override
fun
onDestroy
()
{
isRunning
=
false
cancelLocationUpdates
()
getGoogleServiceJob
?.
cancel
()
getGoogleServiceJob
=
null
super
.
onDestroy
()
}
...
...
@@ -162,7 +182,7 @@ class StayJobService : JobService() {
// 请求位置更新
fusedLocationClient
.
requestLocationUpdates
(
locationRequest
,
it
,
Looper
.
getMainLooper
())
}
getGoogleServiceLocation
()
}
...
...
@@ -175,5 +195,33 @@ class StayJobService : JobService() {
}
}
private
var
getGoogleServiceJob
:
Job
?
=
null
@SuppressLint
(
"MissingPermission"
)
fun
getGoogleServiceLocation
()
{
if
(
getGoogleServiceJob
==
null
)
{
getGoogleServiceJob
=
MainScope
().
launch
(
Dispatchers
.
IO
)
{
while
(
isActive
)
{
fusedLocationClient
.
lastLocation
.
addOnSuccessListener
{
location
:
Location
?
->
if
(
location
!=
null
)
{
val
latLng
=
LatLng
(
location
.
latitude
,
location
.
longitude
)
googleLocation
=
latLng
LogEx
.
logDebug
(
TAG
,
"getGoogleServiceLocation latLng=$latLng"
)
oneShotLocationCallBack
?.
invoke
()
oneShotLocationCallBack
=
null
}
}.
addOnFailureListener
{
e
->
LogEx
.
logDebug
(
TAG
,
"e=$e"
)
}
delay
(
getGoogleServiceLocationDelay
*
1000L
)
}
}
}
}
}
app/src/main/java/com/base/locationsharewhite/ui/locationmap/LocationMapActivity.kt
View file @
f53b130d
...
...
@@ -20,6 +20,7 @@ import com.base.locationsharewhite.location.LocationPositionUtils
import
com.base.locationsharewhite.location.LocationShareListUtils
import
com.base.locationsharewhite.map.MapUtils.addLocationMarker
import
com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import
com.base.locationsharewhite.service.StayJobService
import
com.base.locationsharewhite.service.StayJobService.Companion.requestServiceLocationUpdates
import
com.base.locationsharewhite.ui.set.RenameActivity
import
com.base.locationsharewhite.ui.set.RenameActivity.Companion.RENAME_VIEWING_NICK_NAME
...
...
@@ -270,8 +271,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
val
flag
=
changeMyLocation
(
locationPresenter
.
getMyLocation
(),
true
,
true
,
true
)
if
(!
flag
)
{
locationPresenter
.
getGoogleServiceLocation
()
locationPresenter
.
oneShotLocationCallBack
=
{
StayJobService
.
oneShotLocationCallBack
=
{
changeMyLocation
(
locationPresenter
.
getMyLocation
(),
true
,
true
,
true
)
}
}
...
...
@@ -322,7 +322,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if
(
myMarker
==
null
)
{
var
myLatLng
:
LatLng
?
=
getLatLngByLocationManager
()
if
(
myLatLng
==
null
)
{
myLatLng
=
locationPresenter
.
googleLocation
myLatLng
=
StayJobService
.
googleLocation
}
myLatLng
?.
let
{
LogEx
.
logDebug
(
TAG
,
"getLastKnowLocation ${myLatLng.latitude} ${myLatLng.longitude}"
)
...
...
@@ -404,6 +404,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
override
fun
onPause
()
{
super
.
onPause
()
locationPresenter
.
cancelUploadJob
()
StayJobService
.
getGoogleServiceLocationDelay
=
60
}
override
fun
onResume
()
{
...
...
@@ -415,6 +416,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if
(
isRecreate
)
{
return
}
StayJobService
.
getGoogleServiceLocationDelay
=
30
if
(!
checkLocationPermission
()
||
!
checkBackgroundLocationPermission
())
{
binding
.
ivPermission
.
visibility
=
View
.
VISIBLE
...
...
app/src/main/java/com/base/locationsharewhite/ui/locationmap/LocationPresenter.kt
View file @
f53b130d
...
...
@@ -7,6 +7,8 @@ import android.os.Looper
import
androidx.lifecycle.LifecycleCoroutineScope
import
com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation
import
com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import
com.base.locationsharewhite.service.StayJobService
import
com.base.locationsharewhite.service.StayJobService.Companion.oneShotLocationCallBack
import
com.base.locationsharewhite.service.StayJobService.Companion.requestServiceLocationUpdates
import
com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel
import
com.base.locationsharewhite.utils.LogEx
...
...
@@ -30,12 +32,6 @@ class LocationPresenter(
private
var
TAG
=
"LocationPresenter"
private
var
uploadJob
:
Job
?
=
null
//google的地理位置服务
private
var
fusedLocationClient
:
FusedLocationProviderClient
=
LocationServices
.
getFusedLocationProviderClient
(
activity
)
var
googleLocation
:
LatLng
?
=
null
//一次性更新自己的位置
var
oneShotLocationCallBack
:
(()
->
Unit
)?
=
null
//刷新自己的位置
var
refreshLocationCallBack
:
((
latLng
:
LatLng
?)
->
Unit
)?
=
null
...
...
@@ -43,7 +39,7 @@ class LocationPresenter(
fun
getMyLocation
():
LatLng
?
{
var
latLng
=
activity
.
getLatLngByLocationManager
()
if
(
latLng
==
null
)
{
latLng
=
googleLocation
latLng
=
StayJobService
.
googleLocation
}
return
latLng
}
...
...
@@ -52,7 +48,6 @@ class LocationPresenter(
if
(
uploadJob
==
null
)
{
uploadJob
=
lifecycleCoroutineScope
.
launch
(
Dispatchers
.
IO
)
{
while
(
isActive
)
{
getGoogleServiceLocation
()
val
latLng
=
getMyLocation
()
...
...
@@ -69,22 +64,6 @@ class LocationPresenter(
}
}
@SuppressLint
(
"MissingPermission"
)
fun
getGoogleServiceLocation
()
{
fusedLocationClient
.
lastLocation
.
addOnSuccessListener
{
location
:
Location
?
->
if
(
location
!=
null
)
{
val
latLng
=
LatLng
(
location
.
latitude
,
location
.
longitude
)
googleLocation
=
latLng
oneShotLocationCallBack
?.
invoke
()
oneShotLocationCallBack
=
null
}
}.
addOnFailureListener
{
e
->
LogEx
.
logDebug
(
TAG
,
"e=$e"
)
}
}
fun
cancelUploadJob
()
{
uploadJob
?.
cancel
()
...
...
app/src/main/java/com/base/locationsharewhite/ui/main/MainPresenter.kt
View file @
f53b130d
...
...
@@ -6,6 +6,7 @@ import android.location.Location
import
android.os.Looper
import
androidx.lifecycle.LifecycleCoroutineScope
import
com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import
com.base.locationsharewhite.service.StayJobService
import
com.base.locationsharewhite.utils.LogEx
import
com.google.android.gms.location.FusedLocationProviderClient
import
com.google.android.gms.location.LocationCallback
...
...
@@ -26,22 +27,6 @@ class MainPresenter(
private
val
TAG
=
"MainPresenter"
//google的地理位置服务
private
var
fusedLocationClient
:
FusedLocationProviderClient
=
LocationServices
.
getFusedLocationProviderClient
(
activity
)
private
var
googleLocation
:
LatLng
?
=
null
@SuppressLint
(
"MissingPermission"
)
private
fun
getGoogleServiceLocation
()
{
fusedLocationClient
.
lastLocation
.
addOnSuccessListener
{
location
:
Location
?
->
LogEx
.
logDebug
(
TAG
,
"getGoogleServiceLocation location=$location"
)
if
(
location
!=
null
)
{
val
latLng
=
LatLng
(
location
.
latitude
,
location
.
longitude
)
googleLocation
=
latLng
LogEx
.
logDebug
(
TAG
,
"getGoogleServiceLocation latLng=$latLng"
)
}
}
}
/**
* 获取自身位置,
...
...
@@ -50,7 +35,7 @@ class MainPresenter(
var
latLng
=
activity
.
getLatLngByLocationManager
()
LogEx
.
logDebug
(
TAG
,
"getMyLocation latLng=$latLng"
)
if
(
latLng
==
null
)
{
latLng
=
googleLocation
latLng
=
StayJobService
.
googleLocation
}
return
latLng
}
...
...
@@ -64,7 +49,6 @@ class MainPresenter(
locationJob
=
lifecycleCoroutineScope
.
launch
(
Dispatchers
.
IO
)
{
while
(
isActive
)
{
getGoogleServiceLocation
()
val
latLng
=
getMyLocation
()
launch
(
Dispatchers
.
Main
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment