Code: Select all
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"
}
}
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)
}
}
}
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)
}
}
}
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()
}
}
Im Moment funktioniert nichts.
Danke im Voraus!