import
android.app.Notification;
import
android.app.NotificationChannel;
import
android.app.NotificationManager;
import
android.app.Service;
import
android.content.Context;
import
android.content.Intent;
import
android.os.Build;
import
android.os.IBinder;
import
androidx.annotation.RequiresApi;
import
androidx.core.app.NotificationCompat;
public
class
ForegroundService
extends
Service {
public
ForegroundService() {
}
@Override
public
IBinder onBind(Intent intent) {
throw
new
UnsupportedOperationException(
"Not yet implemented"
);
}
@Override
public
void
onCreate() {
super
.onCreate();
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(
1
,
new
Notification());
Window window=
new
Window(
this
);
window.open();
}
@Override
public
int
onStartCommand(Intent intent,
int
flags,
int
startId) {
return
super
.onStartCommand(intent, flags, startId);
}
@RequiresApi
(Build.VERSION_CODES.O)
private
void
startMyOwnForeground()
{
String NOTIFICATION_CHANNEL_ID =
"example.permanence"
;
String channelName =
"Background Service"
;
NotificationChannel chan =
new
NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_MIN);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert
manager !=
null
;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder =
new
NotificationCompat.Builder(
this
, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(
true
)
.setContentTitle(
"Service running"
)
.setContentText(
"Displaying over other apps"
)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(
2
, notification);
}
}