问题
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?
回答1:
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.
回答2:
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 ;)
回答3:
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>
来源:https://stackoverflow.com/questions/26604474/start-a-service-automatically-when-app-installed-to-device