我在应用程序中使用服务来锁定状态条。
此服务是粘性的,并在启动启动时运行。
但是我的服务在2-5秒后启动并启动(在启动时)。
这一次我能减一点吗?
发布于 2017-08-02 07:30:46
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>https://stackoverflow.com/questions/45454177
复制相似问题