Start a service automatically when app installed to device [duplicate]

被刻印的时光 ゝ 提交于 2020-01-16 00:27:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!