-1

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

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...
         }
    }
}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
0

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..."
}
Rohit Sharma
  • 1,384
  • 12
  • 19