|
|
|
@ -1,6 +1,10 @@ |
|
|
|
package com.yunqiinnovation.amap_service |
|
|
|
|
|
|
|
import android.content.BroadcastReceiver |
|
|
|
import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.os.Build |
|
|
|
import android.os.Bundle |
|
|
|
import android.util.Log |
|
|
|
import androidx.appcompat.app.AppCompatActivity |
|
|
|
@ -23,7 +27,7 @@ import com.amap.api.navi.model.AimLessModeCongestionInfo |
|
|
|
import com.amap.api.navi.model.AimLessModeStat |
|
|
|
import com.amap.api.navi.model.NaviInfo |
|
|
|
import com.amap.api.navi.model.NaviLatLng |
|
|
|
import com.yunqiinnovation.amap_service.NavigationService.Companion.sharedNavi |
|
|
|
|
|
|
|
|
|
|
|
class NavigationActivity : AppCompatActivity(), AMapNaviListener, AMapNaviViewListener { |
|
|
|
private lateinit var naviView: AMapNaviView |
|
|
|
@ -32,6 +36,7 @@ class NavigationActivity : AppCompatActivity(), AMapNaviListener, AMapNaviViewLi |
|
|
|
private var startpos:String = ""; |
|
|
|
private var endpos:String = ""; |
|
|
|
private var ntype:String = ""; |
|
|
|
private var isReceiverRegistered = false |
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
setContentView(R.layout.activity_navigation) |
|
|
|
@ -60,7 +65,31 @@ class NavigationActivity : AppCompatActivity(), AMapNaviListener, AMapNaviViewLi |
|
|
|
} |
|
|
|
navi?.addAMapNaviListener(this); |
|
|
|
|
|
|
|
// 绑定当前 activity 到导航状态 |
|
|
|
|
|
|
|
// 注册广播监听器 |
|
|
|
val filter = IntentFilter().apply { |
|
|
|
addAction("com.yunqiinnovation.amap_service.EXIT_NAVIGATION") |
|
|
|
} |
|
|
|
|
|
|
|
// Android 14+ 需要指定 flag |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
|
|
|
registerReceiver(controlReceiver, filter, Context.RECEIVER_NOT_EXPORTED) |
|
|
|
} else { |
|
|
|
registerReceiver(controlReceiver, filter) |
|
|
|
} |
|
|
|
isReceiverRegistered = true |
|
|
|
} |
|
|
|
|
|
|
|
private val controlReceiver = object : BroadcastReceiver() { |
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
|
|
Log.i("NavigationActivity", "接受到通知事件 ${intent?.action}") |
|
|
|
when (intent?.action) { |
|
|
|
"com.yunqiinnovation.amap_service.EXIT_NAVIGATION" -> { |
|
|
|
Log.d("NavigationService", "收到退出导航广播,准备停止服务") |
|
|
|
finish() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override fun onResume() { |
|
|
|
@ -80,6 +109,10 @@ class NavigationActivity : AppCompatActivity(), AMapNaviListener, AMapNaviViewLi |
|
|
|
NavigationServiceHelper.startNavigation(context,"stop") |
|
|
|
naviView.onDestroy() |
|
|
|
super.onDestroy() |
|
|
|
if (isReceiverRegistered) { |
|
|
|
unregisterReceiver(controlReceiver) |
|
|
|
isReceiverRegistered = false |
|
|
|
} |
|
|
|
Log.i("NavigationActivity", "liwei------------------- onDestroy!") |
|
|
|
} |
|
|
|
|
|
|
|
|