立即启动java android上的服务

立即启动java android上的服务,java,android,service,Java,Android,Service,我使用应用程序中的服务锁定状态栏 此服务是粘性的,在启动时运行 但我的服务在2-5秒后启动并激活(启动时) 这次我可以减少吗 public class BroadcastReceiverOnBootComplete extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIg

我使用应用程序中的服务锁定状态栏

此服务是粘性的,在启动时运行

但我的服务在2-5秒后启动并激活(启动时)

这次我可以减少吗

public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
            context.startService(serviceIntent);
        }
    }
}
服务

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AndroidServiceStartOnBoot extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
       // here you can add whatever you want this service to do
    }

}
AndroidManifest.xml

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

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

    <service android:name=".AndroidServiceStartOnBoot"></service>

服务

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AndroidServiceStartOnBoot extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
       // here you can add whatever you want this service to do
    }

}
AndroidManifest.xml

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

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

    <service android:name=".AndroidServiceStartOnBoot"></service>



你不能。系统正在管理流程。没有办法。您无法控制何时提供服务started@VladMatvienko有许多应用程序在启动时可以快速启动。这些应用程序是如何工作的?为什么您认为其他应用程序启动得更快?可能是由于您的代码,您的服务需要太长时间才能初始化。您不能。系统正在管理流程。没有办法。您无法控制何时提供服务started@VladMatvienko有许多应用程序在启动时可以快速启动。这些应用程序是如何工作的?为什么您认为其他应用程序启动得更快?可能是由于您的代码,您的服务需要太长时间才能初始化。我知道,我需要在启动时快速运行服务android@VladMatvienko对请注意,我需要在开机时快速运行服务。答案的哪一部分使它启动更快?@VladMatvienko Really使用广播接收器的优先级在开机完成后立即启动服务。我知道,我需要在开机时快速运行服务android@VladMatvienko是的,请注意,我需要在开机时快速运行服务。答案的哪一部分使它启动更快?@VladMatvienko Reallyuse priority for broadcast receiver在开机完成后立即启动服务。