Reputation: 19270
I want to start a foreground service that doesn't show notification,
apps like instagram, telegram, zapya, ... have a foreground service and they show no notifications.
I have tested ways like answers
here, but android shows a notification that yourAppName is running
.
I want to know how is that possible to have a foreground service with no notification or warning from OS?
Upvotes: 4
Views: 6938
Reputation: 11
In Android, you cannot run a foreground service without a notification because the OS enforces it. However, you can achieve background execution using JobScheduler, which does not require a visible notification.
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
new Thread(() -> {
// Your background task here
jobFinished(params, false);
}).start();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}
To schedule this job:
JobInfo jobInfo = new JobInfo.Builder(1, new ComponentName(this, MyJobService.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfo);
Upvotes: 0
Reputation: 19270
Generally, you can not define foreground service without a notification. However, user can hide the notification of your app, so you can ask, but if the service gets started with no notification, it'll be killed by OS as soon as OS felt that it needs to be stopped.
TL; DR
No. For background task execution, the Android WorkManager is the newest and most reliable.
Upvotes: 0
Reputation: 42417
Here's an up-to-date technique to make it work on all Android versions:
I know this involves more work than you might like, but it does work, in my experience.
Upvotes: 0