I want to run my android application in background after installing. I tried just running the application in the background. But I have to start the app by myself after rebooting the device. What I need is I need to work my app as FACEBOOK,whatsapp doing. As my knowledge they are running in the background forever and they do not need to have a restart manually every time after a reboot. Someone to help me?
-
3Just implement `android.intent.action.BOOT_COMPLETED` broadcast receiver – Chirag Savsani Sep 26 '18 at 09:36
-
Boot complete listener `android.intent.action.BOOT_COMPLETED` can do the trick. – Rohit5k2 Sep 26 '18 at 09:37
-
Check [this](https://stackoverflow.com/a/6392009/5978440) – Sniffer Sep 26 '18 at 09:39
-
This can be help you out... [Go here](https://stackoverflow.com/questions/19345008/need-code-example-on-how-to-run-an-android-service-forever-in-the-background-eve) – Rashedul Islam Emon Sep 26 '18 at 09:40
2 Answers
In AndroidManifest.xml
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Create Java File named with BootUpReceiver
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//Do your coding here...
}
}
}

- 6,020
- 4
- 38
- 74
You can use FirebaseMessagingService class and use it to do your tasks which ever you want inside it , it can be used to do other things also rather than creating push notifications as it runs in background always you can configure your code at the server end to do several tasks I have used FirebaseMessaging service class to do various background tasks of the application And once it is started it always remains in the background as it is built to send push notifications and they can appear any time.
You just need to send the parameters from Firebase server and write the code upon that received parameters inside your onMessageReceived method in the class
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
int userCount=remoteMessage.getData("count");
//do anything using the parameters which are in remote message
}
}
You need to make a Post Request to the firebase url and send the data inside a Json Object with the following data where to is the access token
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"count": 5
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

- 1,384
- 12
- 19