Created
November 2, 2024 14:47
-
-
Save Rohit-554/f38d22cef5a147a1bec7d345146a7998 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
| import android.app.Notification | |
| import android.app.NotificationChannel | |
| import android.app.NotificationManager | |
| import android.app.PendingIntent | |
| import android.app.Service | |
| import android.content.Intent | |
| import android.content.pm.PackageManager | |
| import android.location.Location | |
| import android.media.MediaPlayer | |
| import android.os.Build | |
| import android.os.IBinder | |
| import android.util.Log | |
| import androidx.core.app.NotificationCompat | |
| import androidx.core.app.ActivityCompat | |
| class ForegroundService : Service() { | |
| private lateinit var mediaPlayer: MediaPlayer | |
| private var isPlayingAudio = false | |
| private val locationTracker = LocationTracker() // Assume this is your location tracking implementation | |
| override fun onCreate() { | |
| super.onCreate() | |
| createNotificationChannel() | |
| startLocationTracking() | |
| } | |
| override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
| val action = intent?.action | |
| when (action) { | |
| ACTION_START_LOCATION_TRACKING -> startLocationTracking() | |
| ACTION_START_PLAYING_AUDIO -> startPlayingAudio() | |
| ACTION_STOP_PLAYING_AUDIO -> stopPlayingAudio() | |
| ACTION_STOP_SERVICE -> stopSelf() | |
| } | |
| return START_STICKY | |
| } | |
| private fun startLocationTracking() { | |
| if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
| // Request permission logic here | |
| return | |
| } | |
| // Assuming locationTracker starts tracking the location | |
| locationTracker.startTracking() | |
| // Start foreground service with location tracking | |
| startForeground(NOTIFICATION_ID, createNotification("Tracking location")) | |
| } | |
| private fun startPlayingAudio() { | |
| if (!isPlayingAudio) { | |
| mediaPlayer = MediaPlayer.create(this, R.raw.your_audio_file) // Your audio file resource | |
| mediaPlayer.setOnCompletionListener { | |
| stopPlayingAudio() | |
| } | |
| mediaPlayer.start() | |
| isPlayingAudio = true | |
| // Update foreground service with media playback | |
| startForeground(NOTIFICATION_ID, createNotification("Playing audio")) | |
| } | |
| } | |
| private fun stopPlayingAudio() { | |
| if (isPlayingAudio) { | |
| mediaPlayer.stop() | |
| mediaPlayer.release() | |
| isPlayingAudio = false | |
| // Update notification if location tracking is still active | |
| startForeground(NOTIFICATION_ID, createNotification("Tracking location")) | |
| } | |
| } | |
| private fun createNotification(contentText: String): Notification { | |
| val notificationIntent = Intent(this, MainActivity::class.java) // Your main activity | |
| val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0) | |
| return NotificationCompat.Builder(this, CHANNEL_ID) | |
| .setContentTitle("Fitness App") | |
| .setContentText(contentText) | |
| .setSmallIcon(R.drawable.ic_notification) // Your app icon | |
| .setContentIntent(pendingIntent) | |
| .setPriority(NotificationCompat.PRIORITY_DEFAULT) | |
| .build() | |
| } | |
| private fun createNotificationChannel() { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| val channel = NotificationChannel( | |
| CHANNEL_ID, | |
| "Foreground Service Channel", | |
| NotificationManager.IMPORTANCE_DEFAULT | |
| ) | |
| val manager = getSystemService(NotificationManager::class.java) | |
| manager.createNotificationChannel(channel) | |
| } | |
| } | |
| override fun onBind(intent: Intent?): IBinder? { | |
| return null | |
| } | |
| companion object { | |
| private const val NOTIFICATION_ID = 1 | |
| private const val CHANNEL_ID = "ForegroundServiceChannel" | |
| const val ACTION_START_LOCATION_TRACKING = "ACTION_START_LOCATION_TRACKING" | |
| const val ACTION_START_PLAYING_AUDIO = "ACTION_START_PLAYING_AUDIO" | |
| const val ACTION_STOP_PLAYING_AUDIO = "ACTION_STOP_PLAYING_AUDIO" | |
| const val ACTION_STOP_SERVICE = "ACTION_STOP_SERVICE" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment