I need to start a service at boot time. I searched a lot. They are talking about Broadcastreceiver. As I am new to android development, I didn\'t get a clear picture about s
First register a receiver in your manifest.xml file:
<receiver android:name="com.mileagelog.service.Broadcast_PowerUp" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
and then write a broadcast for this receiver like:
public class Broadcast_PowerUp extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(context, "Service_PowerUp Started",
Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(context, "Service_PowerUp Stoped", Toast.LENGTH_LONG)
.show();
}
}
}
It's possible to register your own application service for starting automatically when the device has been booted. You need this, for example, when you want to receive push events from a http server and want to inform the user as soon a new event occurs. The user doesn't have to start the activity manually before the service get started...
It's quite simple. First give your app the permission RECEIVE_BOOT_COMPLETED. Next you need to register a BroadcastReveiver. We call it BootCompletedIntentReceiver.
Your Manifest.xml should now look like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> <receiver android:name=".BootCompletedIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".BackgroundService"/> </application> </manifest>
As the last step you have to implement the Receiver. This receiver just starts your background service.
package com.jjoe64; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import com.jjoe64.BackgroundService; public class BootCompletedIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent pushIntent = new Intent(context, BackgroundService.class); context.startService(pushIntent); } } }
From http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html
Pls check JobScheduler for apis above 26
WakeLock was the best option for this but it is deprecated in api level 26
Pls check this link if you consider api levels above 26
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context,%20android.content.Intent)
It says
As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).
so as it says cosider JobScheduler
https://developer.android.com/reference/android/app/job/JobScheduler
if it is to do something than to start and to keep it you can receive the broadcast ACTION_BOOT_COMPLETED
If it isn't about foreground pls check if an Accessibility service could do
another option is to start an activity from broadcast receiver and finish it after starting the service within onCreate() , since newer android versions doesnot allows starting services from receivers