2 years ago

#51218

test-img

cyild_7

How to bring Compose for Wear OS App to foreground when app is in background or when screen is locked?

I want to create a simple Countdown App and when onFinish() is called I want my custom vibration to run (and bring app to foreground) even if app is running in background or screen is locked. Following version is working fine when app is active. What is the proper way (in terms of power management) to implement customVibrate()?

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val START_TIME = 5000L
        setContent {
            var time by remember { mutableStateOf(START_TIME) }

            val timer = object : CountDownTimer(START_TIME, 1000L) {
                override fun onTick(millisUntilFinished: Long) {
                    time = millisUntilFinished
                }

                override fun onFinish() {
                    time = 0L
                    //customVibrate()
                }
            }

            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(text = time.toString())
                Button(onClick = { timer.start() }) {
                    Text(text = "Start")
                }
            }
        }
    }
}

android

kotlin

android-jetpack-compose

wear-os

compose-wear

0 Answers

Your Answer

Accepted video resources