Created
July 25, 2024 18:23
-
-
Save CristianLlanos/2af47d99ef40f9083ad9edf4cf58d081 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adviewer | |
import android.app.ActivityManager | |
import android.app.Notification | |
import android.app.NotificationChannel | |
import android.app.NotificationManager | |
import android.app.PendingIntent | |
import android.app.Service | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Build | |
import android.os.Handler | |
import android.os.IBinder | |
import android.util.Log | |
import androidx.core.app.NotificationCompat | |
class ForegroundService : Service() { | |
companion object { | |
const val CHANNEL_ID = "ForegroundServiceChannel" | |
const val CHANNEL_ID_REOPEN = "CaptiViewForegroundServiceChannel" | |
const val CHECK_INTERVAL: Long = 60000 // 1 minute | |
const val PACKAGE_NAME = "com.adviewer" | |
} | |
private val handler = Handler() | |
private val checkActivityRunnable = object : Runnable { | |
override fun run() { | |
Log.d("ForegroundService", "Checking MainActivity") | |
if (!isActivityRunning(MainActivity::class.java)) { | |
Log.d("ForegroundService", "MainActivity is not running") | |
val notification = NotificationCompat.Builder(this@ForegroundService, CHANNEL_ID_REOPEN) | |
.setContentTitle("Starting CatiView") | |
.setContentText("One of the CatiView activities is not running") | |
.setSmallIcon(R.drawable.ic_launcher_foreground) | |
val pendingIntent = PendingIntent.getActivity( | |
this@ForegroundService, | |
0, | |
Intent(this@ForegroundService, MainActivity::class.java), | |
PendingIntent.FLAG_UPDATE_CURRENT | |
) | |
notification.setContentIntent(pendingIntent) | |
startForeground(1, notification.build()) | |
} | |
handler.postDelayed(this, CHECK_INTERVAL) | |
} | |
} | |
override fun onCreate() { | |
super.onCreate() | |
createNotificationChannel() | |
handler.post(checkActivityRunnable) | |
} | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID) | |
.setContentTitle("CatiView Foreground Service") | |
.setContentText("CaptiView is running in the foreground") | |
.setSmallIcon(R.drawable.ic_launcher_foreground) | |
.build() | |
startForeground(1, notification) | |
// Do your background work here | |
return START_STICKY | |
} | |
override fun onBind(intent: Intent?): IBinder? { | |
return null | |
} | |
private fun createNotificationChannel() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
val serviceChannel = NotificationChannel( | |
CHANNEL_ID, | |
"CaptiView Foreground Service Channel", | |
NotificationManager.IMPORTANCE_DEFAULT | |
) | |
val serviceChannelReopen = NotificationChannel( | |
CHANNEL_ID_REOPEN, | |
"CaptiView Reopen Foreground Service Channel", | |
NotificationManager.IMPORTANCE_DEFAULT | |
) | |
val manager = getSystemService(NotificationManager::class.java) | |
manager.createNotificationChannel(serviceChannel) | |
manager.createNotificationChannel(serviceChannelReopen) | |
} | |
} | |
private fun isActivityRunning(activityClass: Class<*>): Boolean { | |
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager | |
val runningTasks = activityManager.getRunningTasks(Int.MAX_VALUE) | |
for (task in runningTasks) { | |
if (activityClass.name == task.topActivity?.className) { | |
return true | |
} | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment