我对Android开发还是个新手.我想问一个问题,一旦我点击按钮,这个代码就会打开设置应用程序.我想为安装在我的手机上的以下演示应用程序https://github.com/Picovoice/porcupine/tree/master/demo/android/Activity相同.为什么打不开呢?我需要在我的 list 文件中添加一些东西吗?提前谢谢你了.
这是包名称为"com.android.sets"的代码,但不适用于"com.android.youtube".
package com.example.jkfdsjlk
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.jkfdsjlk.ui.theme.JkfdsjlkTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
JkfdsjlkTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
MyApp(
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
}
@Composable
fun OpenAppButton() {
val context = LocalContext.current
// Create a launcher for activity result
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { }
Button(
onClick = {
// Intent to open another app
val intent = context.packageManager.getLaunchIntentForPackage("ai.picovoice.porcupine.demo")
// Check if the intent is not null and launch the app
intent?.let {
launcher.launch(intent)
}
},
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
content = {
Text(text = "Open Another App")
}
)
}
@Composable
fun MyApp(modifier: Modifier) {
Surface(color = MaterialTheme.colorScheme.background) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OpenAppButton()
}
}
}