KOTLIN - Führen Sie eine Funktion in der Mainaktivität von außen aus Mainaktivität ausAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 KOTLIN - Führen Sie eine Funktion in der Mainaktivität von außen aus Mainaktivität aus

Post by Anonymous »

Ich hoffe, das ist einfach. Ich habe eine App, die derzeit über ein Vollbildschirme verfügt. Jede Sekunde wird die Zeit aktualisiert. Als Test habe ich eine Doding () -Funktion in MainActivity . Wenn ich dies in der OnCreate nenne, dann wird der Sound beim Starten der App abgespielt. Das funktioniert. Ich habe verschiedene Methoden ausprobiert, die ich beim Googeln gefunden habe, aber alle scheinen die App beim Start zum Absturz zu bringen. < /P>
Hier ist der aktuelle vollständige Code. Ich möchte, dass der doding () in der zweiten letzten Zeile die Doding () -Funktion in der MainActivity ausführt. Für diesen Test wird es jede Sekunde das WAV spielen, aber dies ist nur, um die Logik zum Laufen zu bringen. Sobald dies funktioniert, werde ich die Logik hinzufügen, damit sie alle x Minuten Zeit spricht, die der Benutzer angibt. < /P>

Code: Select all

package com.example.talkingclockversion1

import android.media.MediaPlayer
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.talkingclockversion1.ui.theme.TalkingClockVersion1Theme
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.time.Duration.Companion.seconds

class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
TalkingClockVersion1Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
// Hide both the navigation bar and the status bar
// This is deprecated, but newer code does not get rid of the three dots at the center of the top of the screen
window.decorView.apply {
systemUiVisibility =
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
}
SetScreenBackgroundToBlack()
UpdateTime()
}
}
}
doDing()
}

fun doDing(){
var mediaPlayer = MediaPlayer.create(this, R.raw.f_ding)
mediaPlayer.start()
}
}

//runs every second to update the time
@RequiresApi(Build.VERSION_CODES.O)
class TimeViewModel : ViewModel() {
@RequiresApi(Build.VERSION_CODES.O)
private val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
private val _current = MutableStateFlow("")
val current = _current.asStateFlow()

init {
viewModelScope.launch {
while (true) {
tick()
delay(1.seconds)

}
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun tick() {
_current.value = LocalDateTime.now().format(formatter)
}
}

//gets the current time, formats it, and passes it to the display function
@Composable
@RequiresApi(Build.VERSION_CODES.O)
fun UpdateTime() {
val viewModel: TimeViewModel = viewModel()
val current by viewModel.current.collectAsStateWithLifecycle()

TimeDisplay(
name = current,
modifier = Modifier.fillMaxWidth()
)
}

//clears the screen background to black
@Composable
fun SetScreenBackgroundToBlack() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black)
)
}

//draws the actual time display
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun TimeDisplay(name: String, modifier: Modifier = Modifier) {
//the space above the text increases by 5 pixels every minute to help avoid burn-in
var currentMinute = LocalDateTime.now().format(DateTimeFormatter.ofPattern("mm"))
var topBufferLines = 400+currentMinute.toInt()*5
Text(
text = "$name",
color = Color.LightGray,
fontSize = 300.sp,
//lineHeight = 500.sp,
lineHeight = topBufferLines.sp,
textAlign = TextAlign.Center,
modifier = modifier.background(Color.Black)
)
doDing()
}
Kann mir jemand helfen>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post