title: How to build an Android app as system service
body: <p>I am facing a problem with an app that is built to run as Android service and should start on boot without any user interaction or existing activity.</p>
  
  <p>From other sources, we know that this is basically not possible: <a href="http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html" rel="nofollow">http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html</a></p>
  
  <p>Currently for testing purposes the application has a very simple GUI and it registers a broadcast receiver as follows:</p>
  
  <pre><code>public class MyAppReceiver extends BroadcastReceiver
  {
      @Override
      public void onReceive(Context context, Intent intent) {
          if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
              Intent serviceIntent = new Intent(context, MyAppService.class);
              context.startService(serviceIntent);
          }
      }
  }
  </code></pre>
  
  <p>This works great if the user launches the app at least once in order to allow the receiver to get <code>ACTION_BOOT_COMPLETED</code>.</p>
  
  <p>The goal is to remove the GUI completely and yet have the application starting on boot without any action needed from the user. Note that this software <strong>will not</strong> be deployed on the Play Store but rather it's built to be part of a custom AOSP build not for public usage.</p>
  
  <p>The only solution I found is to build the application as system service by adding it to the Android application framework.</p>
  
  <p>See references:</p>
  
  <p><a href="http://www.androidenea.com/2009/12/adding-system-server-to-android.html" rel="nofollow">http://www.androidenea.com/2009/12/adding-system-server-to-android.html</a>
  <a href="http://processors.wiki.ti.com/index.php/Android-Adding_SystemService" rel="nofollow">http://processors.wiki.ti.com/index.php/Android-Adding_SystemService</a></p>
  
  <p>However I am a bit confused about the advantages and disadvantages of this possibility and I hope someone can answer the following questions:</p>
  
  <p><strong>1)</strong> Does the app need to be built with the whole system (AOSP from source) and therefore it cannot be deployed as APK?</p>
  
  <p><strong>2)</strong> Is there any way to update the application afterwards (with an APK) once the system has been built?</p>
  
  <p><strong>3)</strong> Is it even possible or convenient to run the app this way (as system service) purely because it needs to start on boot and not because of other reasons (such as instantiating code from it etc.)?</p>
  
  <p>The app itself is nothing special - it's a client/remote-server model that reports system statistics for research purposes.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Adding the manifest for completeness:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.myapp.myapp"
    android:versionCode="1"
    android:versionName="22.0"&gt;
  
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-permission android:name="android.permission.GET_TASKS"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application android:label="@string/app_name" android:icon="@drawable/ic_launcher"&gt;
  
      &lt;activity android:name="myapp"
                android:label="@string/app_name"&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;service
          android:name="org.myapp.myappclient.myappservice"
          android:label="myappservice"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="org.myapp.myappclient.myappservice" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  
      &lt;receiver
          android:enabled="true"
          android:name="org.myapp.myappclient.myappreceiver"
          android:permission="android.permission.RECEIVE_BOOT_COMPLETED"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>