This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add foreground service in android example
- Loading branch information
Showing
5 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/example/fishjamandroidexample/CameraService.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.fishjamandroidexample | ||
|
||
import android.app.ForegroundServiceStartNotAllowedException | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.content.pm.ServiceInfo | ||
import android.os.Build | ||
import android.os.IBinder | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.ServiceCompat | ||
import androidx.core.content.ContextCompat | ||
|
||
class CameraService : Service() { | ||
private fun startForeground() { | ||
// Before starting the service as foreground check that the app has the | ||
// appropriate runtime permissions. In this case, verify that the user has | ||
// granted the CAMERA permission. | ||
val cameraPermission = | ||
ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) | ||
if (cameraPermission == PackageManager.PERMISSION_DENIED) { | ||
// Without camera permissions the service cannot run in the foreground | ||
// Consider informing user or updating your app UI if visible. | ||
stopSelf() | ||
return | ||
} | ||
|
||
try { | ||
val channelID = "FISHJAM_NOTIFICATION_CHANNEL" | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
// Create the NotificationChannel. | ||
val name = "Fishjam notification channel" | ||
val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
val mChannel = NotificationChannel(channelID, name, importance) | ||
// Register the channel with the system. You can't change the importance | ||
// or other notification behaviors after this. | ||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.createNotificationChannel(mChannel) | ||
} | ||
val notification = NotificationCompat.Builder(this, channelID) | ||
// Create the notification to display while the service is running | ||
.setContentTitle("Fishjam is running") | ||
.build() | ||
ServiceCompat.startForeground( | ||
/* service = */ this, | ||
/* id = */ 100, // Cannot be 0 | ||
/* notification = */ notification, | ||
/* foregroundServiceType = */ | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA | ||
} else { | ||
0 | ||
}, | ||
) | ||
} catch (e: Exception) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
&& e is ForegroundServiceStartNotAllowedException | ||
) { | ||
// App not in a valid state to start foreground service | ||
// (e.g. started from bg) | ||
} | ||
// ... | ||
} | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
startForeground() | ||
return START_STICKY | ||
} | ||
override fun onBind(intent: Intent): IBinder? { | ||
return null | ||
} | ||
} |
This file contains 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
This file contains 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