How can you make an app that does not contain Activity
as main launcher, but contain service as main launcher?

- 4,701
- 8
- 43
- 62

- 11
- 2
-
possible duplicate of [android app with service only](http://stackoverflow.com/questions/990217/android-app-with-service-only) – An SO User Oct 02 '14 at 07:45
1 Answers
Yes it is possible. Just don't define any activity in your AndroidManifest and you're fine.
What you should do however to be able for your services to be called, is to listen on some phone-actions with a broadcast receiver.
Something like this:
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
The actions BOOT_COMPLETED and MY_PACKAGE_REPLACED are triggered when the user's device is rebooted (obviously) but also whenever an upgrade of your application (or initial install) is done (via the Play Store for example). Then you can start your service again (or schedule an alarm for you service to be triggered x minutes/hours/days/...).
If you don't have any possible user input, another useful action is ACTION_USER_PRESENT. This one will be triggered whenever your user unlocks his phone.
CAUTION: For the BOOT_COMPLETED action you need to add a permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then in your 'MyBroadcastReciever' class you should not do anything special to check the actions. You know you will only get in there by the actions specified in the manifest, so your broadcast receiver will look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do your work here...
}
}
All services that will be ever triggered should be defined in the manifest also! Don't forget about that!

- 1,364
- 1
- 17
- 30