[英]Starting service on boot immediately java android
I use service in my app for lock statusbar. 我在我的应用程序中将服务用于锁定状态栏。
This service is sticky and run on boot start 此服务是粘性的,可在启动时运行
But my service start after 2-5 second and actived (on boot start). 但是我的服务在2-5秒后启动并处于活动状态(在启动启动时)。
Can i decrease this time? 我可以减少这个时间吗?
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);
}
}
}
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 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.