Search code examples
androidservicebroadcastreceiver

i want run service and broadcast receiver after the force close the application


How can I automatically restart my service and broadcast receiver after the application gets force close due to some error. Since I'm using inbuilt applications like calender and events. Some of the mobile doesnt support and getting force closed. How to handle those things and restart those service and broadcast receiver.

Thanks


Solution

  • Register your receiver in the AndroidManifest. That way it will be called even when your app is not running: R.styleable.AndroidManifestReceiver

    You could then check in the BroadcastReceiver whether the service is still running and restart it.

    See for example the class SmSForwarder here. The broadcastreceiver does not need to run because it is registerend in the AndroidManifest.xml (see line 29). Android will start the Broadcastreceiver as soon as the Intent "android.provider.Telephony.SMS_RECEIVED" is sent.