Progress Praktikum-1
This commit is contained in:
parent
362a79d591
commit
86ba20f78c
@ -57,4 +57,8 @@ dependencies {
|
||||
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
|
||||
debugImplementation(libs.androidx.compose.ui.tooling)
|
||||
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
||||
implementation("androidx.compose.material3:material3:1.1.1")
|
||||
|
||||
}
|
||||
@ -2,6 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
@ -12,7 +14,7 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.PanicButton">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:name="com.example.panicbutton.MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.PanicButton">
|
||||
|
||||
@ -1,47 +1,88 @@
|
||||
package id.ac.ubharajaya.panicbutton
|
||||
|
||||
package com.example.panicbutton
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import id.ac.ubharajaya.panicbutton.ui.theme.PanicButtonTheme
|
||||
import androidx.compose.ui.unit.dp
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
PanicButtonTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
Greeting(
|
||||
name = "Android",
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
)
|
||||
}
|
||||
}
|
||||
MyApp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text = "Hello $name!",
|
||||
modifier = modifier
|
||||
)
|
||||
fun MyApp() {
|
||||
// State untuk menampilkan hasil request
|
||||
var message by remember { mutableStateOf("Klik tombol untuk mengirim notifikasi") }
|
||||
|
||||
|
||||
// UI
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(text = message, Modifier.padding(bottom = 16.dp))
|
||||
Button(onClick = {
|
||||
// Kirim HTTP request saat tombol ditekan
|
||||
sendNotification { response ->
|
||||
message = response
|
||||
}
|
||||
}) {
|
||||
Text(text = "Kirim Alert")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun GreetingPreview() {
|
||||
PanicButtonTheme {
|
||||
Greeting("Android")
|
||||
}
|
||||
}
|
||||
|
||||
// Fungsi untuk mengirimkan HTTP request
|
||||
fun sendNotification(onResult: (String) -> Unit) {
|
||||
val client = OkHttpClient()
|
||||
val url = "https://ntfy.ubharajaya.ac.id/panic-button" // Ganti <your-topic> dengan topik Anda
|
||||
|
||||
|
||||
|
||||
|
||||
val requestBody = RequestBody.create(
|
||||
"text/plain".toMediaType(), // Mengirim plain text
|
||||
"Notifikasi dari Arif R D!" // Pesan yang akan tampil
|
||||
)
|
||||
|
||||
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(requestBody)
|
||||
.build()
|
||||
|
||||
|
||||
// Eksekusi request di thread terpisah
|
||||
Thread {
|
||||
try {
|
||||
val response = client.newCall(request).execute()
|
||||
if (response.isSuccessful) {
|
||||
onResult("Notifikasi berhasil dikirim!")
|
||||
} else {
|
||||
onResult("Gagal mengirim notifikasi: ${response.code}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
onResult("Error: ${e.message}")
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user