title: Why are my intents not delivered correctly?
body: <p>I'm having problems starting a service in a android app. The following code should work well. But often, not always, the Intent of the <code>startService</code> command is not correctly delivered to the <code>onStartCommand</code> method. This means that the intent is received by the service class but the extras are lost:</p>
  
  <pre><code>D/TestService: onStartCommand() called with: intent = [Intent { cmp=***/.services.hostConnection.TestService launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } (has extras) }], flags = [0], startId = [6]
  D/TestService: onStartCommand: Bundle[mParcelledData.dataSize=0]
  </code></pre>
  
  <p>Also I'm getting sometimes a <code>RuntimeException: Unable to create service ***.services.hostConnection.TestService</code> (full stack trace below)</p>
  
  <p>I'm seaching for days now but can't find this bug...</p>
  
  <p>A minimalist service class which reproduces the problem:</p>
  
  <pre><code>public class TestService extends Service {
      private static final String TAG = "TestService";
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
          if(intent != null &amp;&amp; intent.getExtras() != null) Log.d(TAG, "onStartCommand: " + intent.getExtras().toString());
          return START_NOT_STICKY;
      }
  }
  </code></pre>
  
  <p>The method which starts the service:</p>
  
  <pre><code>public void startLongUpdates(long hostId){
      Log.d(TAG, "startLongUpdates() called");
  
      Intent intent = new Intent(Application.getInstance(), TestService.class).putExtra(Constants.SERVICE_ACTION_INTENT_EXTRA, HostConnectionService.LONG_UPDATE).putExtra(Constants.CONNECTED_HOST_ID_INTENT_EXTRA, hostId);
      Log.d(TAG, "startLongUpdates: " + intent.getExtras().toString());
      Application.getInstance().startService(intent);
  
  }
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: ***, PID: 24974
                                                             java.lang.RuntimeException: Unable to create service ***.services.hostConnection.TestService: java.lang.SecurityException: Binder invocation to an incorrect interface
                                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
                                                                 at android.app.ActivityThread.-wrap6(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                              Caused by: java.lang.SecurityException: Binder invocation to an incorrect interface
                                                                 at android.os.Parcel.readException(Parcel.java:1693)
                                                                 at android.os.Parcel.readException(Parcel.java:1646)
                                                                 at android.hardware.display.IDisplayManager$Stub$Proxy.getDisplayInfo(IDisplayManager.java:718)
                                                                 at android.hardware.display.DisplayManagerGlobal.getDisplayInfo(DisplayManagerGlobal.java:143)
                                                                 at android.hardware.display.DisplayManagerGlobal.getCompatibleDisplay(DisplayManagerGlobal.java:200)
                                                                 at android.hardware.display.DisplayManagerGlobal.getRealDisplay(DisplayManagerGlobal.java:214)
                                                                 at android.app.ContextImpl.createAppContext(ContextImpl.java:2183)
                                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:3499)
                                                                 at android.app.ActivityThread.-wrap6(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6688) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
  </code></pre>
  
  <p>Also sometimes this exception appears:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: ***, PID: 5355
                                                           java.lang.RuntimeException: Unable to start service ***.services.hostConnection.TestService@9386a65 with Intent { cmp=***/.services.hostConnection.TestService (has extras) }: java.lang.RuntimeException: Unknown exception code: 2 msg ServiceActionIntentExtra
                                                               at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
                                                               at android.app.ActivityThread.-wrap17(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                            Caused by: java.lang.RuntimeException: Unknown exception code: 2 msg ServiceActionIntentExtra
                                                               at android.os.Parcel.readException(Parcel.java:1634)
                                                               at android.os.Parcel.readException(Parcel.java:1573)
                                                               at android.app.ActivityManagerProxy.serviceDoneExecuting(ActivityManagerNative.java:3814)
                                                               at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3019)
                                                               at android.app.ActivityThread.-wrap17(ActivityThread.java) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:148) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="***"&gt;
  
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:name="***.Application"
      android:allowBackup="false"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
  
      &lt;activity
          android:launchMode="singleTop"
          android:name="***.gui.menu.MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"&gt;
      &lt;/activity&gt;
      &lt;activity
          android:name=".gui.SplashActivity"
          android:theme="@style/SplashTheme"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;activity
          android:name="***.gui.menu.KnownHostInfoActivity"
          android:theme="@style/AppTheme.NoActionBar" /&gt;
  
      &lt;service android:name="***.services.hostConnection.TestService" /&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>Application.java:</p>
  
  <pre><code>public class Application extends android.app.Application{
      private static final String TAG = "Application";
      private static Application application;
      public static Application getInstance(){
          return application;
      }
      public void onCreate() {
          super.onCreate();
          application = this;
      }
  }
  </code></pre>