Against what @ sven-menschner said, I think the unbound Service is exactly what you need, as related services are subject to the bind / unbind mechanisms that will kill your service. What would I do:
In your manifest file, define your service:
<service android:name=".YourService" android:enabled="true" android:exported="true" android:description="@string/my_service_desc" android:label="@string/my_infinite_service"> <intent-filter> <action android:name="com.yourproject.name.LONGRUNSERVICE" /> </intent-filter> </service>
Note Here is a list of actions already implemented, but you can define your own actions for the intention to start the service. Just create a singleton class and define the strings by assigning them a String , which must be unique. βEnabledβ set to true is just an instance of the service, and the exported set is true only if you need other applications that send intentions to your Service . If not, you can safely set the latter to false.
The next step is to start the service from your activity. This is easy to do:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent servIntent = new Intent("com.yourproject.name.LONGRUNSERVICE"); startService(servIntent); ... } }
The final step is to define your Service initializations. Watch out for the onBind() method. Since you do not want this to be related, just return null . It will be something like this:
public class MyService extends Service { @Override public IBinder onBind(Intent intent) {
Now your service will be launched, even if you close your main Activity . There is only one step left: to prevent your Service from ending, start it as a front-end service (do it in your service). This will basically create a notification icon in the status bar. This does not mean that your main activity also works (therefore, you do not need a related service), since Activities and Services have different life cycles. To help this service work for so long, try to keep your heap as low as possible so that it avoids Android SO killing it.
Another invitation: you cannot check if the Service continues to kill DVM. If you kill DVM, you will kill everything, and thereby the Service.