知识库

wiki.linsakura.com

用户工具

站点工具


wiki:dev:android:android_service开机启动

这是本文档旧的修订版!


一、修改AndroidManifest.xml

在文件中添加 android:persistent="true"

AndroidManifest.xml
...
    <application>
        android:persistent="true"
        android:name="com.example.myservice.application.MyApplication"
        ...
        <service>
            android:name="com.example.myservice.MyServie"
            ...
        </service>
    </application>
...

二、修改Application代码

在onCreate中添加:

MyApplication.java
public class MyApplication extends Application{
    public void onCreate(){
        super.onCreate();
        //add-begin
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(com.example.myservice, com.example.myservice.MyService));
        startServiceAsUser(intent, UserHandle.OWNER);
        //end-add
    }
}
wiki/dev/android/android_service开机启动.1647255541.txt.gz · 最后更改: 2022/03/14 18:59 由 博丽幻月