Java. Android Studio.Running the service in the background without notification

Hello everyone, please tell me how to start the service in the background without notification in the app?

And what does it mean that my notification is constantly updated, how can I fix it?

Now I use this code:

private void startForeground() {
    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    startForeground(NOTIF_ID, new NotificationCompat.Builder(this, 
            NOTIF_CHANNEL_ID) // don't forget create a notification channel first
            .setOngoing(true)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Service is running background")
            .setContentIntent(pendingIntent)
            .build());
}

Thank you in advance.

Author: ЮрийСПб, 2020-09-19

1 answers

Starting with Android version 8+, you can't run any background tasks without explicitly notifying the user.

In order for you to run some scheduled tasks, Google provides Work Manager.

Here is a lesson on how to use it.

 0
Author: Sergei Buvaka, 2020-09-19 20:15:44