I would not use the android: process attribute - this actually starts your service in a separate process and makes it difficult to perform such actions. You do not have to worry about your service dying, when your application dies, the service will continue to work (this is a service point). You also do not want the binding service, because it will start and die when the bindings are performed. It is said:
<service android:enabled="true" android:exported="true" android:name=".MyService" android:label="@string/my_service_label" android:description="@string/my_service_description" <intent-filter> <action android:name="com.package.name.START_SERVICE" /> </intent-filter> </service>
You can define your own action for the intention to start the service (cannot be started by the system - there must be an event). I like to set “enabled” to true so that the system can create an instance of my service and “export”, so that other applications can send intentions and interact with my service. Read more about it here .
You can also create a long service that uses bindings, if you do, just add and configure for the binder, for example:
<action android:name="com.package.name.IRemoteConnection" />
Now, to start the service from your activity:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent serviceIntent = new Intent("com.package.name.START_SERVICE"); this.startService(serviceIntent);
Now for the service:
public class MyService extends Service { @Override public IBinder onBind(Intent intent) {
Your service should now work. To help with longevity, there is some kind of trick. Make it work as a front-end service (you do it in the service) - this will force you to create a notification icon, which is located in the status bar. Also keep the HEAP light on to make your app less likely to be killed by Android.
In addition, the service is killed when you kill DVM, so if you are going to Settings-> Applications and stop the application, you kill DVM and thus kill the service. Just because you see that your application is running in the settings does not mean that the activity is running. Activities and services have different life cycles, but can share DVM. Just keep in mind that you don’t need to kill your activity, if you don’t need to, just let Android handle it.
Hope this helps.