OnStart Command vs onStart

The onStartCommand method was introduced with Android 2.0 (API level 5). Previous to that, the method used was onStart. onStart's parameters are an intent and an int for startId. It does not include the int flags parameter and doesn't have a return. If you are targeting a phone that is running something earlier than 2.0, you can use the onStart method.

@Override public void onStart(Intent intent, int startid) { Log.v("SIMPLESERVICE","onStart");

The onDestroy method is called when the OS is destroying a Service. In this example, it is triggered when the stopService method is called by our activity. This method should be used to do any cleanup that needs to be done when a Service is being shut down.

public void onDestroy() {


Finally, in order to make this example work, we need to add an entry to our manifest file (AndroidManifest.xml) that specifies our Service.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apress.proandroidmedia.ch06.simpleservice" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SimpleServiceActivity" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<service android:name=".SimpleServiceService" />


<uses-sdk android:minSdkVersion="5" /> </manifest>

Of course, this example doesn't do anything other than output to the log indicating when the Service has been started and stopped. Let's move forward and have our Service actually do something.

+3 0


  • Mirabella
    How we call onstartcommand() in onstart in android?
    4 years ago

Post a comment