I have developed an Android application that includes a background service (MainService) designed to run automatically after the device restarts. To achieve this functionality, I have implemented a BootReceiver class and added it to the AndroidManifest.xml file to handle the ACTION_BOOT_COMPLETED intent. However, despite these efforts, the MainService is not starting automatically after the device restarts.
I have created a BroadcastReceiver class named BootReceiver and added it to my AndroidManifest.xml file to start a background service (MainService) after the device restarts. However, the service is not starting automatically after the device restarts. Below are the relevant parts of my code:
BootReceiver.java:
package com.sithum.mediatracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// Start your service here
Intent serviceIntent = new Intent(context, MainService.class);
context.startService(serviceIntent);
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MediaTracker"
tools:targetApi="31">
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".MainService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="dataSync|location|mediaPlayback"
android:permission="android.permission.BIND_JOB_SERVICE">
<!-- Intent filter if needed -->
<intent-filter>
<action android:name="com.sithum.mediatracker.ACTION_START_SERVICE" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have also declared the necessary permissions in the manifest file. And I tested this in android 9 and 11. What could be causing the background service not to start automatically after device restart, and how can I resolve this issue?