mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 09:27:41 +08:00
优化:避免个别机型重启后自启动时startService
可能空指针导致crash
This commit is contained in:
parent
fded1d1e3c
commit
51b77d4c85
@ -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 集成双进程前台服务,JobScheduler,onePix(一像素),WorkManager,无声音乐
|
//Cactus 集成双进程前台服务,JobScheduler,onePix(一像素),WorkManager,无声音乐
|
||||||
@ -218,19 +223,15 @@ 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 ->
|
||||||
}
|
CactusSave.timer = aLong
|
||||||
.subscribeOn(Schedulers.io())
|
CactusSave.date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).run {
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
format(Date())
|
||||||
.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))
|
|
||||||
}
|
}
|
||||||
|
mTimer.value = dateFormat.format(Date(aLong * 1000))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
Thread.sleep(500)
|
appContext?.stopService(it)
|
||||||
appContext?.startService(Intent(appContext, HttpService::class.java))
|
Thread.sleep(500)
|
||||||
|
appContext?.startService(it)
|
||||||
|
}
|
||||||
refreshButtonText()
|
refreshButtonText()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,10 +257,12 @@ class ServerFragment : BaseFragment<FragmentServerBinding?>(), View.OnClickListe
|
|||||||
checkCallPermission()
|
checkCallPermission()
|
||||||
checkContactsPermission()
|
checkContactsPermission()
|
||||||
checkLocationPermission()
|
checkLocationPermission()
|
||||||
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
Intent(appContext, HttpService::class.java).also {
|
||||||
appContext?.stopService(Intent(appContext, HttpService::class.java))
|
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
||||||
} else {
|
appContext?.stopService(it)
|
||||||
appContext?.startService(Intent(appContext, HttpService::class.java))
|
} else {
|
||||||
|
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))
|
||||||
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
Intent(appContext, HttpService::class.java).also {
|
||||||
appContext?.stopService(Intent(appContext, HttpService::class.java))
|
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
||||||
appContext?.startService(Intent(appContext, HttpService::class.java))
|
appContext?.stopService(it)
|
||||||
} else {
|
Thread.sleep(500)
|
||||||
appContext?.startService(Intent(appContext, HttpService::class.java))
|
appContext?.startService(it)
|
||||||
|
} else {
|
||||||
|
appContext?.startService(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
refreshButtonText()
|
refreshButtonText()
|
||||||
true // allow selection
|
true // allow selection
|
||||||
|
Loading…
x
Reference in New Issue
Block a user