How to start a Service when .apk is Installed for the first time
Asked Answered
A

11

70

In my Application I am not having any UI part, so I need to start a Service as soon as the Applicaton gets installed on the Device. I saw many links from which the answer was that its not possible but I guess it is surely possible. Just have a look at PlanB Application on the Android Market that does fulfil my requirement. Below is my Manifest file how I tried, but the Service was not called at all. So, let me know what is the best possible way to start a Service when the Application gets Installed.

UPDATE

I also tried using android.intent.action.PACKAGE_ADDED it works fine for detecting the Package for the other Applications but not for itself.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.auto.start"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher" >

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

        <receiver android:name=".BootUpReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>
Augur answered 16/12, 2011 at 9:7 Comment(8)
Maybe this post will help you: #2228104?Nina
@Lalit: Investigate what happens with a PUSH installation of an application from the Market and what it can do. Something is triggering Plan B to start - it isn't starting automatically. I've just installed it and I'm wondering if the installation sent something to their servers which then sent an email to my device. A BroadcastReceiver listening for incoming emails from a particular email address could be used to start a Service. After that, I don't understand enough about how PUSH installations from the Market work or how they install without user input so can't help further.Disobedience
@Lalit: Hi,Have you found any way to start service when apk installed for first time?Welldefined
No, its not possible without any UI part in the application.Augur
Thanks for quick response.I have one more doubt.so is it possible to create application without any app icon?means the application consists of only broadcast receivers and services.Welldefined
Yes, possible but it will work below 3.1 or 3.1.Augur
Yes,I have read that from answer of CommonsWare.and that is why I asked you if you found anything to work around it for higher versions.because right now most of the devices are launched with ICS or JellyBean.anyways,Thanks for Response!Welldefined
@MehulJoisar <data android:scheme="package" android:pathPattern="com.example.packagename" />.I put piece of code in my Androidmanifest file it will hide app from menu.Hope this is what you need.Kelsi
N
80

Fortunately, Plan B does not work on Android 3.1+, as tested on a XOOM and a Galaxy Nexus.

What Plan B does is exploit a security hole that could be used by drive-by malware, which is specifically why Android prevents it from happening anymore.


UPDATE

To clarify: As inazaruk posted and I put into comments on other answers, all applications, upon installation, are placed in a "stopped" state. This is the same state that the application winds up in after the user force-stops the app from the Settings application. While in this "stopped" state, the application will not run for any reason, except by a manual launch of an activity. Notably, no BroadcastReceviers will be invoked, regardless of the event for which they have registered, until the user runs the app manually.

This block covers the Plan B scenario of remote-install-and-run, which they were taking advantage of previously. After all, with that, anyone with a hacked Google account would be at risk of having their device infected, hands-free as it were.

So, when the OP says:

I need to start a Service as soon as the Applicaton gets installed on the Device

the OP will be unsuccessful and will need to redesign the application to avoid this purported "need".

Nitrosyl answered 16/12, 2011 at 13:45 Comment(1)
"Notably, no BroadcastReceviers will be invoked, regardless of the event for which they have registered, until the user runs the app manually" or the hardware action is performed. This can include launching the camera or a reboot (as the author also mentions in https://mcmap.net/q/281301/-how-to-start-android-service-on-installation). Keep in mind, this may easily get your app flagged as malware on Google Play.Poacher
O
18

Applications installed on the /system partition are not subject to being placed into the "stopped" state after installation. If you have root, you can do,

$ adb root
$ adb remount
$ adb push your.apk /system/app

And it can immediately receive broadcast intents. This certainly doesn't provide a general purpose solution, but i wanted to mention it for completeness.

EDIT: Keep in mind that different versions of Android locate system APKs in different places. For example, Android 8 puts them under /system/app//.apk. Shell into your device and poke around and follow the same scheme used for other system APKs.

Oliva answered 6/1, 2012 at 20:36 Comment(7)
,If he do so he will not be able to update the apps ,,, every time he has to do the same procedure....Intwine
as i mentioned, this is not a general purpose solution.Oliva
Of course he will be able to update a system app. As long as you have the same package name and signing you can update a system app. The update will be placed on the data partition but still have the "system" status since a previous version is available on the system partition.Wilford
@Wilford the point is, he's NOT a system app. this isn't a general purpose solution because it requires that the app is installed on the system partition to begin with. unless you manufacturer android devices, this is pretty hard to accomplish.Oliva
@JeffreyBlattman Yes, I agree with your points. I was commenting on the comment from Vipin. I disagreed with his statement that every update had to be done via the adb commands. My point was that they only needs to be done once. Once the apk is in /system/app normal app install/update can be used.Wilford
@JeffreyBlattman : does it applies only for apps installed on the "regular" data partition, but signed with the system signature?Kerriekerrigan
I had been signing my app with the platform certificate but installing it "normally" using adb. It wasn't until I pushed the apk to /system/app that the service started on its own. This answer was very helpful.Gorge
U
14

I agree with CommonsWare's answer to question: How to start android service on installation. In other words, you can't automatically start your service after you've just been installed.

One more thing about newer Android platforms: if you don't have UI at all, you'll have trouble starting your service even when using BOOT_COMPLETE intent on Android 3.1+.

That's because all installed applications are in stopped state. In this state applications will not receive ANY broadcast notifications.

In order to activate your application some other application (or user) needs to start your service or activity, or content provider. The usual workflow is when user clicks on your application's icon.

I've written a detailed explanations about this in my blog post.

Unger answered 16/12, 2011 at 10:30 Comment(4)
You gave a link to the wrong blog-post, right one is “Activating” applications. Cheers :)Worriment
links not found@ inazaruk,Alexander MalakhovIncrust
@Unger : Have you a new link for your blog post?Demodulation
@AjayPandya, @Kikiwa, @AADTechnical, latest archived link.Unswear
I
5

Plan B does this launch by listening to the events which happen in the system. It uses a receiver which literally listenes to hundreds of events hoping that some of them will eventually fire up. So this is how you can do it. Otherwise, there are no built-in means to launch the application as soon as it gets installed.

Imminence answered 16/12, 2011 at 10:5 Comment(8)
Oh!! so listening to 100's of event would cost highest battery usage, so the App. can fail, I don't think that can be the case.Augur
@Malcolm: +1 - I agree. There's some 'external' trigger being listened for by a BroadcastReceiver. I'm wondering if it could be an email from their servers but by 'external' it could simply be some other 'event' on the device itself.Disobedience
@Lalit: "...cost highest battery usage, so the App. can fail...". I just installed Plan B and it automatically enabled GPS on my phone. Have you ever seen how fast GPS will drain the battery on an HTC Desire??? A lot faster than listening with a passive BroadcastReceiver for various system events.Disobedience
Well, the system doesn't provide a method to do this, so the way Plan B uses is an obvious hack, which is not even guaranteed to work. Of course, Plan B intends to work on the lost device, so all means are good. But this isn't the method to be used in the apps for everyday use, that's for sure. You can listen to less events, though it reduces the chances that your application starts soon.Imminence
@MisterSquonk " if it could be an email from their servers" but I am thinking that how would the server know that the App. is installed and an email has t be sent?Augur
@LalitPoptani It's easier to have the user send an SMS to his device in the case of Plan B, they don't really need such a complex mechanism.Imminence
@Imminence hmm then too sending sms needs some code to be called when the app has installed. :)Augur
@LalitPoptani Plan B is supposed to get installed on the device which is currently lost, so a user also sends an SMS himself. I didn't mean that it's sent automatically.Imminence
D
5

I'm not sure what your constraints/purpose is, but if you can install another application that has an activity you can have it send an intent with the flag FLAG_INCLUDE_STOPPED_PACKAGES.

This will use your application for the intent resolution, even though it's in a stopped state. If the action of the intent matches one of your filters, it will also bring the package out of the stopped state.

Delamare answered 29/7, 2013 at 20:57 Comment(0)
G
4

I don't think so You can start service immediately after installed your application on device,

The application must first be invoked by the user through some sort of Activity.The only things you have to register some Broadcast Receiver with appropriate intents in manifest which invoke you service when something is happening on device but this remaing to Android 3.1 version.

EDIT:

After Android 3.1+ onwards you can not use any Broadcast for starting your application, because all application remains in inactive state after completion of device boot and to launch the application the user have to invoke it.(By touching the app icon).

Gaskin answered 16/12, 2011 at 9:31 Comment(6)
then each time a new app gets installed, you will start your own application....:D.Ruggles
@Ruggles don't worry about that I will check the Package name of the app. installed in that case. :DAugur
ya sure you will, but all a hacker needs is this broadcast and sends the installed packaged data to remote server.Ruggles
"The surely one is the on RECEIVE_BOOT_COMPLETE, which one you already tried." -- not on Android 3.1+. The user has to manually launch an activity from the application before any BroadcastReceiver will work.Nitrosyl
@Nitrosyl so RECEIVE_BOOT_COMPLETE won't work for the first time Application is installed, but will work thereafter if the user has used the applciation once, is it so?Augur
@LalitPoptani: Once the user launches an activity of your app (e.g., via the launcher), all BroadcastReceivers will work, until the user force-stops the app from the Settings app, and the app moves back to the "stopped" state it was in when first installed. It is possible that task killers will also have the same effect -- I haven't tried this.Nitrosyl
A
1

As stated by CommonsWare in the answer to this question (which I suppose you have all ready seen, but chose to ignore) starting a Service on install is not possible - it is simply not a thing that is implemented into the platform.

Starting it automaticly at the next boot is however possible.

As stated in the Technical Details for PlanB:

Plan B will attempt to launch as soon as it downloads, but in some cases you will need to send an SMS to get it started.

My guess is that on a rooted phone you might be able to start the Service on install - but there's no guarantee that the phone is rooted, which is why PlanB will require recieving a text in some cases because that can be registered by the IntentFilter of the app and then used to start the Service.

Alic answered 16/12, 2011 at 9:33 Comment(2)
yes checked that but you can check the PlanB application that I had linked in the question.Augur
"Starting it automaticly at the next boot is however possible." -- not on Android 3.1+. The user has to manually launch an activity from the application before any BroadcastReceiver will work.Nitrosyl
C
1

there is an app on google play Android Lost which invoke the registration service for google push messages via an incoming sms without launching the app even once for version 3.0+.

Cestar answered 25/7, 2013 at 8:45 Comment(0)
P
1

Perhaps the best way to accomplish this (and now I'm speaking to the specific intent of the OP, a program that gets installed in order to retrieve a stolen phone, not the general question) is social engineering, not software engineering.

So, an icon with text like "Password List" or "My Bank Accounts" which suddenly appeared on the home screen would undoubtedly be clicked on. Look at the success of all sorts of other phishing, and here you would be targeting a thief, who's already motivated to continue nefarious activity. Let the thief start it for you. :)

Poetize answered 31/7, 2013 at 20:49 Comment(0)
L
0

There is a way to do this.

As you've already mentioned you can use android.intent.action.PACKAGE_ADDED it works fine for detecting the Package for the other Applications. The one last step is that finding another app (which already running on device) to query and launch your application.

If your targeting users have installed any apps you're cooperated, or look around and find some common services pre-installed on everyone's device (I couldn't say more, but this method has been used by thousands of apps).

Leader answered 23/3, 2023 at 9:33 Comment(0)
E
-5

HEY I think using a BroadcastRecivier to automatically start the app on restart of device hence it will automatically start on device start.Hope this will help

Excerpt answered 29/11, 2017 at 18:32 Comment(1)
This is not an truthful/accurate answer, check the chosen answer to understand why this won't work.Bukhara

© 2022 - 2024 — McMap. All rights reserved.