Android Service Not Getting Start From Jobintentservice On Boot
I am trying to run a service on OREO device and service get started as it listens to android.intent.action.BOOT_COMPLETED intent. Below is Boot Received Broadcast Reciever class: p
Solution 1:
Since Android O Apps can no longer run Background Services while the App is in the Background. You will need to either update to a foreground service or migrate to jobs. I recommend the Evernote Android Job library to simplify working with Jobs and backwards compatibility.
Solution 2:
Yes. You can use the foreground service temporally to run background service.
I attached my code as below.
This is BOOT_COMPLETE BroadcastReceiver class.
publicclassBootCompleteReceiverextendsBroadcastReceiver {
privatestaticfinalStringTAG="BootCompleteReceiver";
@OverridepublicvoidonReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intenti=newIntent(context, TempForegroundService.class);
context.startForegroundService(i);
} else {
Intenti=newIntent(context, BackgroundService.class);
context.startService(i);
}
}
}
This is TempForegroundService class.
publicclassTempForegroundServiceextendsService {
@OverridepublicvoidonCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
StringNOTIFICATION_CHANNEL_ID="Your Package Name";
StringchannelName="Your Channel Name";
NotificationChannelchan=newNotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManagermanager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.BuildernotificationBuilder=newNotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notificationnotification= notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
startService(newIntent(this, BackgroundService.class));
stopForeground(true);
stopSelf();
}
}
@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Nullable@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
}
And this is BackgroundService class.
publicclassBackgroundServiceextendsService {
publicfinalstaticStringTAG="BackgroundService";
publicSyncService() {
}
@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
@OverridepublicvoidonCreate() {
super.onCreate();
}
@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
// Add your codereturn START_STICKY;
}
}
Please don't forget to add permissions and define services and receiver.
<uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permissionandroid:name="android.permission.FOREGROUND_SERVICE" /><serviceandroid:name=".BackgroundService"android:enabled="true"android:exported="false" /><serviceandroid:name=".TempForegroundService"android:enabled="true"android:exported="false" /><receiverandroid:name=".BootCompleteReceiver"><intent-filter><actionandroid:name="android.intent.action.REBOOT" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter></receiver>
Post a Comment for "Android Service Not Getting Start From Jobintentservice On Boot"