Android在应用程序启动时启动系统服务

Android在应用程序启动时启动系统服务,android,broadcastreceiver,Android,Broadcastreceiver,在AndroidManifest.xml中,我向“android.intent.action.BOOT_COMPLETED”事件注册了一个BroadcastReceiver。 这将在每次Android设备启动时启动服务 ... <receiver android:name="MySystemScheduleReceiver" > <intent-filter> <action android:n

在AndroidManifest.xml中,我向“android.intent.action.BOOT_COMPLETED”事件注册了一个BroadcastReceiver。 这将在每次Android设备启动时启动服务

...
        <receiver android:name="MySystemScheduleReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
...
如果是,我应该使用哪种方法?(onCreate,onResume…)


谢谢

您可以对具有onCreate()回调的应用程序进行子类化。你可以在那里开始你的服务

public class MyApplication extends Application {
    public void onCreate() {
        // start your service
    }
}

在清单的
标记中,添加
android:name=“MyApplication”

,您可以对应用程序进行子类化,该应用程序具有onCreate()回调。你可以在那里开始你的服务

public class MyApplication extends Application {
    public void onCreate() {
        // start your service
    }
}

在清单的
标记中,添加
android:name=“MyApplication”

“但是,我希望在安装应用程序后立即启动系统服务,而无需重新启动设备”——系统服务是固件的一部分,而不是应用程序。请编辑您的问题,使其完整准确地说明您所指的内容。您是否有任何GUI活动?如果是,您可以从活动启动onCreate方法上的服务,如下所示:
Intent service=new Intent();setAction(“com.myapp.MyService”);启动服务(服务)我将sendBroadcast调用放在onCreate(主要GUI活动)中,它似乎工作正常。谢谢。“但是,我希望在应用程序安装后立即启动系统服务,而无需重新启动设备”——系统服务是固件的一部分,而不是应用程序。请编辑您的问题,使其完整准确地说明您所指的内容。您是否有任何GUI活动?如果是,您可以从活动启动onCreate方法上的服务,如下所示:
Intent service=new Intent();setAction(“com.myapp.MyService”);启动服务(服务)我将sendBroadcast调用放在onCreate(主要GUI活动)中,它似乎工作正常。谢谢
public class MyApplication extends Application {
    public void onCreate() {
        // start your service
    }
}