8

I want to start a service in an APK.

I tried to use as following:

<application android:icon="@drawable/icon" android:label="@string/app_name">  
        <service android:name =".TestServcie">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </service>  
    </application>

Any ideas?
Thanks

NetStarter
  • 3,189
  • 7
  • 37
  • 48
Sean
  • 6,785
  • 7
  • 23
  • 26
  • So I take it it doesn't work? (Not because you spelled Service wrong?) Does anything happen at all? Btw, are you planning ANY feedback to the user that the service started? – EboMike Dec 17 '10 at 05:44

2 Answers2

30

You can write a BroadcastReceiver and run the Service after receiving the Intent. For example after device boot-up or other Intent that you need.

<receiver android:name=".StartupReceiver">
   <intent-filter>
     <action android:name="android.intent.action.BOOT_COMPLETED"/>
     <category android:name="android.intent.category.HOME"/>
   </intent-filter>
</receiver>
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
mysuperass
  • 1,199
  • 1
  • 9
  • 10
  • I think this answer is the right one. It describes how to start a service without action from the user and I think that's what the author was looking for. – Undo Jan 13 '13 at 22:32
  • As much as I Google, it seems like user does actually have to run the application in order for the receivers to be registered. – Pijusn Jan 17 '13 at 08:53
  • 1
    In this case, the receivers are registered via AndroidManifest.xml . In other words they are registered when the user installs the app. – Qlimax Aug 02 '13 at 14:32
  • what's `StartupReceiver`? – Incerteza Apr 01 '15 at 01:14
10

No you can't.

Create a simple Activity which starts the service and simply provides some feedback to the user (to tell them the service has started for example) and set that Activity with the MAIN/LAUNCHER intent.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 6
    Someone remove this terrible and unhelpful answer and put the other one on top. – Arijoon Sep 02 '14 at 14:15
  • 1
    @Arijoon : How do you propose the `Service` is started? From Android v3.x onwards it's not possible to start any app component unless the user has manually started the app from the launcher - that means starting an `Activity`. Most importantly, it would be a bad design to start app components without the users' permission. Also, the other answer may be correct BUT it will fail unless an `Activity` has been started by the user (thereby indicating they give their permission). It will also fail if the app is manually stopped and not manually restarted again or if the app is installed on SD card. – Squonk Sep 02 '14 at 16:16
  • 1
    It is only required to run an Activity **once** after install. Thereafter you can start the service on boot. First part of your answer is correct however the rest of it is not helpful. Many services like Viber run on boot in the background without asking the user. – Arijoon Sep 03 '14 at 08:14
  • 1
    @Arijoon : If a user manually stops an app using Settings -> Apps it will *NEVER* be started again until the user manually launches the app using an `Activity` which has been registered as the MAIN / LAUNCHER `Activity`. Also, if you look at the original question title it says "...without activity **or receiver**? The other answer says to use a `BroadcastReceiver` and it doesn't answer the question in the way it was asked - that's why I said it can't be done without using an `Activity` or a `BroadcastReceiver`. – Squonk Sep 03 '14 at 11:56
  • 1
    **ONLY IF** the user stops the app through Settings > Apps as you said! Thats is not a very common case!! There are many applications that take advantage of this for example Viber, Fb Messenger, Snapchat! They all start on boot simply because you have launched them once, and until you manually **FORCE STOP** them they will be running in the background. I simply asked the more useful answer be moved up and your answer which is not helpful as an *answer* be removed or moved to comments. (Someone with the same question would not like your answer) – Arijoon Sep 03 '14 at 12:10
  • 2
    @Arijoon : Please read the original question thoroughly. It's not possible to do what the OP was asking using the AndroidManifest they posted. Also, as I said, the OP wanted to know if it could be done "without activity or receiver". Also, if you really feel my answer is unhelpful, incorrect or spam then feel free to down-vote it and/or flag it for moderators attention to have it deleted. The reason my answer appears above the other is because the OP accepted it - there's nothing stopping Stack Overflow users from scrolling down to see other answers - that's how Stack Overflow works. – Squonk Sep 03 '14 at 17:44
  • 1
    made Stackoverflow more entertaining. – ralphgabb Aug 27 '19 at 06:40