You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.5 KiB
Kotlin

2 years ago
package com.yinuo.safetywatcher
import android.content.Intent
2 years ago
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.yinuo.safetywatcher.navi.NavigationUtil
import com.yinuo.safetywatcher.navi.NavigationView
import com.yinuo.safetywatcher.ui.SplashView
import com.yinuo.safetywatcher.ui.theme.EasypusherTheme
import org.easydarwin.push.MediaStream
2 years ago
class MainActivity : ComponentActivity() {
var exitTime = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startMediaService()
2 years ago
setContent {
EasypusherTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val viewModel: MainViewModel = viewModel()
if (viewModel.isSplash) {
SplashView { viewModel.isSplash = false }
} else {
NavigationUtil.navHostController = rememberNavController()
NavigationView()
}
}
}
}
addBackListener()
}
private fun startMediaService() {
// 启动服务...
val intent = Intent(this, MediaStream::class.java)
startService(intent)
}
2 years ago
private fun addBackListener() {
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
//是主页
if (System.currentTimeMillis() - exitTime > 2000) {
Toast.makeText(this@MainActivity, "再点一次退出!", Toast.LENGTH_SHORT)
.show()
exitTime = System.currentTimeMillis()
} else {
finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
2 years ago
}
}
})
}
}