ForegroundService.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. package TARGET_PACKAGE_NAME;
  19. import android.app.Service;
  20. import android.content.pm.ServiceInfo;
  21. import android.app.Notification;
  22. import android.app.NotificationChannel;
  23. import android.app.NotificationManager;
  24. import android.content.Intent;
  25. import android.os.Build;
  26. import android.os.IBinder;
  27. import android.util.Log;
  28. public class ForegroundService extends Service {
  29. @Override
  30. public void onCreate() {
  31. super.onCreate();
  32. createNotificationChannel();
  33. }
  34. @Override
  35. public int onStartCommand(Intent intent, int flags, int startId) {
  36. //Log.d("darkfi", "ForegroundService::onStartCommand()");
  37. Notification.Builder builder =
  38. new Notification.Builder(this, "darkfi_service");
  39. Notification notification = builder
  40. .setContentTitle("DarkFi")
  41. .setContentText("Running p2p network...")
  42. .setSmallIcon(android.R.drawable.ic_dialog_info)
  43. .build();
  44. startForeground(
  45. 100,
  46. notification,
  47. ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
  48. );
  49. return START_STICKY;
  50. }
  51. @Override
  52. public IBinder onBind(Intent intent) {
  53. return null;
  54. }
  55. private void createNotificationChannel() {
  56. //Log.d("darkfi", "ForegroundService::createNotificationChannel()");
  57. NotificationChannel channel = new NotificationChannel(
  58. "darkfi_service",
  59. "DarkFi Foreground Service",
  60. NotificationManager.IMPORTANCE_DEFAULT
  61. );
  62. NotificationManager manager = getSystemService(NotificationManager.class);
  63. manager.createNotificationChannel(channel);
  64. }
  65. }