Search code examples
javaandroidandroid-background

How to run an application forever after installing?


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?


Solution

  • 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...
             }
        }
    }