by Anonymous » 24 Jul 2025, 16:59
Ich entwickle eine Android-Anwendung mit dem Expo-Framework (keine iOS-Version). Dieser Frame sollte auch schließen, wenn er tippt.
/Mainservice.kt
Code: Select all
package fr.devsoleo.activmotiv
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_DEFAULT
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import fr.devsoleo.activmotiv.popup.PresenceReceiver
class MainService : Service() {
private val receiver = PresenceReceiver()
override fun onCreate() {
super.onCreate()
val filter = IntentFilter(Intent.ACTION_USER_PRESENT)
applicationContext.registerReceiver(receiver, filter)
}
override fun onDestroy() {
applicationContext.unregisterReceiver(receiver)
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startNotification()
return START_STICKY
}
@RequiresApi(Build.VERSION_CODES.O)
private fun startNotification() {
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT).apply {
description = "ActivMotiv Notification Channel"
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
val notification: Notification = Notification.Builder(this, CHANNEL_ID)
.setContentTitle("Notification titre")
.setContentText("Notification contenu")
.build()
startForeground(2, notification)
}
companion object {
const val CHANNEL_ID = "ActivMotivNotif"
const val CHANNEL_NAME = "ActivMotiv"
}
}
/bootreceiver.kt
Code: Select all
class BootReceiver : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val serviceIntent = Intent(context, MainService::class.java)
context.startForegroundService(serviceIntent)
}
}
}
/popup/prreencereceiver.kt
Code: Select all
package fr.devsoleo.activmotiv.popup
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class PresenceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_USER_PRESENT) {
val startIntent = Intent(context, ImagesActivity::class.java)
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(startIntent)
}
}
}
/popup/imageaktivität.KT
Code: Select all
package fr.devsoleo.activmotiv.popup
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
class ImagesActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onRestart() {
super.onRestart()
}
override fun onResume() {
super.onResume()
}
override fun onStop() {
super.onStop()
}
}
Ich habe es geschafft, die automatische Öffnung zum Arbeiten zu bringen (bei leerer Aktivität), aber ich kann nicht auf diese Phase zurückkehren. 53.
Im Moment funktioniert nichts.
Danke im Voraus!
Ich entwickle eine Android-Anwendung mit dem Expo-Framework (keine iOS-Version). Dieser Frame sollte auch schließen, wenn er tippt.[code]
[/code]
[b]/Mainservice.kt[/b]
[code]package fr.devsoleo.activmotiv
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_DEFAULT
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import fr.devsoleo.activmotiv.popup.PresenceReceiver
class MainService : Service() {
private val receiver = PresenceReceiver()
override fun onCreate() {
super.onCreate()
val filter = IntentFilter(Intent.ACTION_USER_PRESENT)
applicationContext.registerReceiver(receiver, filter)
}
override fun onDestroy() {
applicationContext.unregisterReceiver(receiver)
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startNotification()
return START_STICKY
}
@RequiresApi(Build.VERSION_CODES.O)
private fun startNotification() {
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT).apply {
description = "ActivMotiv Notification Channel"
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
val notification: Notification = Notification.Builder(this, CHANNEL_ID)
.setContentTitle("Notification titre")
.setContentText("Notification contenu")
.build()
startForeground(2, notification)
}
companion object {
const val CHANNEL_ID = "ActivMotivNotif"
const val CHANNEL_NAME = "ActivMotiv"
}
}
[/code]
[b]/bootreceiver.kt[/b]
[code]class BootReceiver : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val serviceIntent = Intent(context, MainService::class.java)
context.startForegroundService(serviceIntent)
}
}
}
[/code]
[b]/popup/prreencereceiver.kt[/b]
[code]package fr.devsoleo.activmotiv.popup
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class PresenceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_USER_PRESENT) {
val startIntent = Intent(context, ImagesActivity::class.java)
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(startIntent)
}
}
}
[/code]
[b]/popup/imageaktivität.KT[/b]
[code]package fr.devsoleo.activmotiv.popup
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
class ImagesActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onRestart() {
super.onRestart()
}
override fun onResume() {
super.onResume()
}
override fun onStop() {
super.onStop()
}
}
[/code]
Ich habe es geschafft, die automatische Öffnung zum Arbeiten zu bringen (bei leerer Aktivität), aber ich kann nicht auf diese Phase zurückkehren. 53.
Im Moment funktioniert nichts.
Danke im Voraus!