优化:避免个别机型重启后自启动时startService可能空指针导致crash

This commit is contained in:
pppscn 2023-03-04 23:04:16 +08:00
parent fded1d1e3c
commit 51b77d4c85
4 changed files with 54 additions and 44 deletions

View File

@ -113,24 +113,29 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
}
//启动前台服务
val intent = Intent(this, ForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
Intent(this, ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it)
} else {
startService(it)
}
}
//网络状态监听
val networkStateServiceIntent = Intent(this, NetworkStateService::class.java)
startService(networkStateServiceIntent)
Intent(this, NetworkStateService::class.java).also {
startService(it)
}
//电池状态监听
val batteryServiceIntent = Intent(this, BatteryService::class.java)
startService(batteryServiceIntent)
Intent(this, BatteryService::class.java).also {
startService(it)
}
//启动HttpServer
if (HttpServerUtils.enableServerAutorun) {
startService(Intent(this, HttpService::class.java))
Intent(this, HttpService::class.java).also {
startService(it)
}
}
//Cactus 集成双进程前台服务JobScheduleronePix(一像素)WorkManager无声音乐
@ -218,19 +223,15 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
}
mLastTimer.postValue(dateFormat.format(Date(CactusSave.lastTimer * 1000)))
mEndDate.postValue(CactusSave.endDate)
mDisposable = Observable.interval(1, TimeUnit.SECONDS)
.map {
oldTimer + it
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { aLong ->
CactusSave.timer = aLong
CactusSave.date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).run {
format(Date())
}
mTimer.value = dateFormat.format(Date(aLong * 1000))
mDisposable = Observable.interval(1, TimeUnit.SECONDS).map {
oldTimer + it
}.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe { aLong ->
CactusSave.timer = aLong
CactusSave.date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).run {
format(Date())
}
mTimer.value = dateFormat.format(Date(aLong * 1000))
}
}
override fun onStop() {

View File

@ -105,11 +105,12 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
//启动前台服务
if (!ForegroundService.isRunning) {
val intent = Intent(this, ForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
Intent(this, ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it)
} else {
startService(it)
}
}
}
})

View File

@ -133,11 +133,12 @@ class FrpcFragment : BaseFragment<FragmentFrpcsBinding?>(), FrpcPagingAdapter.On
}
R.id.iv_play -> {
if (!ForegroundService.isRunning) {
val intent = Intent(requireContext(), ForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(intent)
} else {
requireContext().startService(intent)
Intent(requireContext(), ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(it)
} else {
requireContext().startService(it)
}
}
}

View File

@ -236,9 +236,11 @@ class ServerFragment : BaseFragment<FragmentServerBinding?>(), View.OnClickListe
HttpServerUtils.enableApiLocation = isChecked
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
Log.d("ServerFragment", "onClick: 重启服务")
appContext?.stopService(Intent(appContext, HttpService::class.java))
Thread.sleep(500)
appContext?.startService(Intent(appContext, HttpService::class.java))
Intent(appContext, HttpService::class.java).also {
appContext?.stopService(it)
Thread.sleep(500)
appContext?.startService(it)
}
refreshButtonText()
}
}
@ -255,10 +257,12 @@ class ServerFragment : BaseFragment<FragmentServerBinding?>(), View.OnClickListe
checkCallPermission()
checkContactsPermission()
checkLocationPermission()
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
appContext?.stopService(Intent(appContext, HttpService::class.java))
} else {
appContext?.startService(Intent(appContext, HttpService::class.java))
Intent(appContext, HttpService::class.java).also {
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
appContext?.stopService(it)
} else {
appContext?.startService(it)
}
}
refreshButtonText()
}
@ -318,11 +322,14 @@ class ServerFragment : BaseFragment<FragmentServerBinding?>(), View.OnClickListe
HttpServerUtils.serverWebPath = webPath
XToastUtils.info(getString(R.string.restarting_httpserver))
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
appContext?.stopService(Intent(appContext, HttpService::class.java))
appContext?.startService(Intent(appContext, HttpService::class.java))
} else {
appContext?.startService(Intent(appContext, HttpService::class.java))
Intent(appContext, HttpService::class.java).also {
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
appContext?.stopService(it)
Thread.sleep(500)
appContext?.startService(it)
} else {
appContext?.startService(it)
}
}
refreshButtonText()
true // allow selection