Я нашел это, ребята. Однако спасибо.
Main Activity. java
getApplicationContext().startForegroundService(new Intent(dis, foregroundService.class));
foregroundService. java
public class foregroundService extends Service {
private Handler handler = new Handler();
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return null;
}
private void start (){
handler.postDelayed(runnable, 6000);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
Toast.makeText(foregroundService.this, "Refresh", Toast.LENGTH_SHORT).show();
start();
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
// dont show the notification//
startForeground(1,new Notification.Builder(this).build());
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
start();
}
}