优化:避免个别机型重启后自启动时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) Intent(this, ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent) startForegroundService(it)
} else { } else {
startService(intent) startService(it)
}
} }
//网络状态监听 //网络状态监听
val networkStateServiceIntent = Intent(this, NetworkStateService::class.java) Intent(this, NetworkStateService::class.java).also {
startService(networkStateServiceIntent) startService(it)
}
//电池状态监听 //电池状态监听
val batteryServiceIntent = Intent(this, BatteryService::class.java) Intent(this, BatteryService::class.java).also {
startService(batteryServiceIntent) startService(it)
}
//启动HttpServer //启动HttpServer
if (HttpServerUtils.enableServerAutorun) { if (HttpServerUtils.enableServerAutorun) {
startService(Intent(this, HttpService::class.java)) Intent(this, HttpService::class.java).also {
startService(it)
}
} }
//Cactus 集成双进程前台服务JobScheduleronePix(一像素)WorkManager无声音乐 //Cactus 集成双进程前台服务JobScheduleronePix(一像素)WorkManager无声音乐
@ -218,13 +223,9 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
} }
mLastTimer.postValue(dateFormat.format(Date(CactusSave.lastTimer * 1000))) mLastTimer.postValue(dateFormat.format(Date(CactusSave.lastTimer * 1000)))
mEndDate.postValue(CactusSave.endDate) mEndDate.postValue(CactusSave.endDate)
mDisposable = Observable.interval(1, TimeUnit.SECONDS) mDisposable = Observable.interval(1, TimeUnit.SECONDS).map {
.map {
oldTimer + it oldTimer + it
} }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe { aLong ->
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { aLong ->
CactusSave.timer = aLong CactusSave.timer = aLong
CactusSave.date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).run { CactusSave.date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).run {
format(Date()) format(Date())

View File

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

View File

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

View File

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