I need an app which will run always in the background and apps will start while phone is turn on. Please help me with example code. I already tried several code but it run on background while pressing button after start the apps
Asked
Active
Viewed 40 times
0
-
Take a look : https://stackoverflow.com/questions/4562734/android-starting-service-at-boot-time – Remees M Syde Sep 15 '18 at 09:24
1 Answers
0
You need to receive BOOT_COMPLETED
of the phone, then start the service.
Follow the following steps
Step 1: create your service
public class myService extends Service{
public myService(){}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
}
Step 2: Create your boot receiver
public class BootReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, RemindersService.class);
context.startService(i);
}
}
Step 3: add them to manifest inside application
<service
android:name=".services.RemindersService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".services.BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Step 4: add permission in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Thats it. Happy coding. Please note that in android Oreo, you will want to start service as foreground
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
}

Lucem
- 2,912
- 3
- 20
- 33
-
Thank you very much Mr Lucem. If you don't mind , can i get little bit details code ?? – Abeed Mukid Sep 15 '18 at 12:26
-
-