Skip to main content

B. Handling Background Tasks

The Entrupy Android SDK performs network operations, such as uploading images, that can continue even if the application is sent to the background. The SDK handles background processing internally. No additional configuration is required.

1. Background Processing Considerations

Android handles background processing differently than iOS. You do not need to configure WorkManager, foreground services, or custom workers for SDK operations—the SDK manages background work internally.

2. Application Lifecycle Handling

Activity Lifecycle

The SDK automatically handles activity lifecycle events, but you should ensure proper integration:

import com.entrupy.sdk.app.EntrupyApp
import com.entrupy.sdk.listeners.CaptureCallback

class YourActivity : AppCompatActivity(), CaptureCallback {

override fun onPause() {
super.onPause()
// The SDK will continue background operations
// No special handling required for most cases
}

override fun onResume() {
super.onResume()
// The SDK will automatically check for completed background operations
// and trigger appropriate callbacks
}

override fun onDestroy() {
super.onDestroy()
// Clean up any SDK resources if needed
// The SDK handles its own cleanup
}

override fun onCaptureStarted() {
// Handle capture started
}

override fun onCaptureError(errorCode: Int, description: String) {
// Handle capture error
}
}

Application Class Integration

Initialize the SDK once from your Application class:

import android.app.Application
import com.entrupy.sdk.app.EntrupyApp

class YourApplication : Application() {

override fun onCreate() {
super.onCreate()

// Initialize the SDK
EntrupyApp.init(this)

// Register for network state changes if needed
registerNetworkCallback()
}

private fun registerNetworkCallback() {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
Log.d("Network", "Network available - SDK will retry operations if needed")
}

override fun onLost(network: Network) {
Log.w("Network", "Network lost - SDK will queue operations")
}
}

connectivityManager.registerDefaultNetworkCallback(networkCallback)
}
}

3. Background Upload Handling

Automatic Background Processing

The SDK automatically handles background uploads:

import com.entrupy.sdk.app.EntrupyApp
import com.entrupy.sdk.listeners.CaptureCallback
import com.entrupy.sdk.listeners.EntrupyErrorCode
import com.entrupy.sdk.model.METADATA_KEY_BRAND
import com.entrupy.sdk.model.METADATA_KEY_ITEM_TYPE
import com.entrupy.sdk.model.METADATA_KEY_CUSTOMER_ITEM_ID
import com.entrupy.sdk.model.configMetadataOf

// When starting a capture, the SDK handles background processing automatically
val metadata = configMetadataOf(
METADATA_KEY_BRAND to "Nike",
METADATA_KEY_ITEM_TYPE to "Sneakers",
METADATA_KEY_CUSTOMER_ITEM_ID to "INTERNAL_SKU_12345"
)

EntrupyApp.sharedInstance().startCapture(
configMetadata = metadata,
callback = object : CaptureCallback {
override fun onCaptureStarted() {
// Capture UI launched successfully
Log.d("Capture", "Capture started")
}

override fun onCaptureError(errorCode: Int, description: String) {
// Handle errors, including network-related errors
when (errorCode) {
EntrupyErrorCode.SERVICE_UNAVAILABLE -> {
Log.w("Capture", "Service unavailable: $description")
}
else -> {
// Handle other errors
Log.e("Capture", "Capture failed: $description")
}
}
}
}
)
note

The SDK manages its own background task queue. You do not need to implement custom Workers or schedule background tasks for SDK operations.

4. Battery Optimization Considerations

Requesting Battery Optimization Exemption

For critical operations, you may want to request exemption from battery optimization:

class YourActivity : AppCompatActivity() {

private fun requestBatteryOptimizationExemption() {
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val packageName = packageName
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
val intent = Intent().apply {
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
data = Uri.parse("package:$packageName")
}
startActivity(intent)
}
}
}
}

Handling Doze Mode

The SDK is designed to work with Android's Doze mode and App Standby:

// The SDK automatically handles Doze mode and App Standby
// No special configuration required for most use cases

5. Best Practices

5.1 Network Handling

  • Error handling: Use SDK callbacks and error codes to inform users when operations fail (for example, when the service is unavailable).
  • Manual Retry: Offer users a way to retry critical operations from your app UI when appropriate.

5.2 User Experience

  • Progress Indicators: Show appropriate progress indicators for long-running operations.
  • Status Updates: Keep users informed about the status of background operations.
  • Error Handling: Provide clear error messages and recovery options.

5.3 Resource Management

  • Memory Usage: The SDK is optimized for minimal memory usage during background operations.
  • Battery Usage: Background operations are designed to minimize battery impact.
  • Storage: Temporary files are automatically cleaned up after operations complete.

6. Troubleshooting

6.1 Uploads Not Completing

  • Check network connectivity
  • Verify battery optimization settings
  • Ensure the app has necessary permissions

6.2 Callbacks Not Triggered

  • Verify callback registration
  • Check activity lifecycle handling
  • Ensure proper SDK initialization

6.3 Background Processing Delays

  • Check device battery optimization settings
  • Monitor network connectivity

This setup ensures that the SDK can reliably manage its network activity, contributing to a more robust authentication experience for your users.

Next Steps