1

I want to start a service automatically when the app is installed. Service is working properly after a launch or after a device boot. So, is it possible to launch a service after installation of the app?

Sumighosh Charuvil
  • 446
  • 1
  • 4
  • 14

3 Answers3

0

Google shows the correct answer as the first hit so ... have you done some research on this? How to start a Service when .apk is Installed for the first time

Summary: you can't do this.

Community
  • 1
  • 1
Carsten
  • 806
  • 5
  • 6
  • I do researched. And all the posts and comments were old and i was thinking any update in the latest release might do this. However, thanks for your summary. – Sumighosh Charuvil Oct 28 '14 at 09:21
0

Techanically It's not possible to start service when your app install. It's possible to start with Google Glass Development Kit. There is option to install your app through the Voice and also can start service(Voice Trigger command).

 <service
            android:name="com.est.poc.glass.service.POCGlassService"
            android:enabled="true"
            android:exported="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
            </intent-filter>
            <!-- Voice command found in res/xml/voice_trigger_start -->
            <meta-data
                android:name="com.google.android.glass.VoiceTrigger"
                android:resource="@xml/voice_trigger_start" />
            <meta-data
                android:name="com.google.android.glass.voice_trigger"
                android:resource="@string/voice_trigger_title" />
        </service>
samsad
  • 1,241
  • 1
  • 10
  • 15
-1

yes it is possible by listening the broadcast of the installed package

This is your broadcast

public class InstallBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = null;
        if (intent != null) {
            action = intent.getAction();
        }

        if (action != null && Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            String dataString = intent.getDataString();
            if (dataString != null
                    && dataString.equals(YOUR_PACKAGE_NAME)) {

                //Launch your service :)
            }
        }
    }
}

This is your manifest

<receiver
        android:name=".InstallBroadcastReceiver"
        android:enabled="false"
        android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED"/>
        <data android:scheme="package"/>
    </intent-filter>
</receiver>

Hope it helps ;)

Luis Pereira
  • 1,481
  • 3
  • 19
  • 46
  • Have you actually tried this? I would be surprised if your app receives a broadcast that your app has been installed. I am pretty sure that this is broadcasted only to other installed apps. Edit: that is from the documentation: Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast. – Carsten Oct 28 '14 at 10:23
  • It does receive I tested and its working correctly, after the user accepted the install dialog it will come across this broadcast. have a look http://developer.android.com/reference/android/content/Intent.html Try the code before you down vote a question please... – Luis Pereira Oct 28 '14 at 10:36
  • I down voted based upon the documentation (which states that it is not supported) and based on the statements from other users in the duplicate answer where they clearly stated that it does not work. Maybe it works in certain variations but it is not intented to work. – Carsten Oct 28 '14 at 11:03
  • Weird its working in my case, I will investigate further, I will come back to you after. Thanks for your honest answer ;) – Luis Pereira Oct 28 '14 at 11:05
  • I see that it will create race condition. where app is waiting for itself to get notified when itself is getting installed. – Dinesh Prajapati Aug 30 '16 at 14:20
  • Are you sure it didn't appear to work because you were updating the app? It's possible the confirmation you received was the old version receiving notice of the new version before it was replaced. By delaying the actual install, you gave it time to complete what would otherwise be discarded. – Abandoned Cart Jun 21 '19 at 16:44