From f151e203da870dbe281defcb1daf84a1a38ecac1 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期一, 29 十二月 2025 09:59:57 +0800
Subject: [PATCH] Merge branch 'master' into google
---
app/src/main/java/com/hdl/photovoltaic/uni/MyForegroundService.java | 97 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 97 insertions(+), 0 deletions(-)
diff --git a/app/src/main/java/com/hdl/photovoltaic/uni/MyForegroundService.java b/app/src/main/java/com/hdl/photovoltaic/uni/MyForegroundService.java
new file mode 100644
index 0000000..e010d83
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/uni/MyForegroundService.java
@@ -0,0 +1,97 @@
+package com.hdl.photovoltaic.uni;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.Service;
+import android.content.Intent;
+import android.os.Build;
+import android.os.IBinder;
+
+import androidx.annotation.Nullable;
+import androidx.core.app.NotificationCompat;
+
+import com.hdl.photovoltaic.R;
+
+
+
+public class MyForegroundService extends Service {
+ private static final String CHANNEL_ID = "MyForegroundServiceChannel";
+ private static final int NOTIFICATION_ID = 1;
+
+ private static boolean isRunning = false;
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ // 鍒涘缓閫氱煡娓犻亾锛圓ndroid 8.0+锛�
+ createNotificationChannel();
+ isRunning = true;
+
+ }
+// @Override
+// public void onTaskRemoved(Intent rootIntent) {
+// super.onTaskRemoved(rootIntent);
+// isRunning = false;
+
+ /// / // 鍋滄鍓嶅彴鐘舵�侊紝绉婚櫎閫氱煡
+ /// / stopForeground(true); // true琛ㄧず绉婚櫎閫氱煡
+ /// /// // 鍋滄鏈嶅姟
+ /// /// stopSelf();
+// // 1. 鍏堢Щ闄ゅ墠鍙扮姸鎬侊紙鍙�夛紝浣嗗缓璁級
+// stopForeground(true);
+//
+// // 2. 鍋滄鏈嶅姟
+// stopSelf();
+// }
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ // 鍒涘缓閫氱煡
+ Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
+ .setContentTitle("")//瀹夸富杩涚▼鏈嶅姟
+ .setContentText("")//姝e湪涓巙ni-app淇濇寔閫氫俊
+ .setSmallIcon(R.drawable.notification_logo) // 蹇呴』璁剧疆涓�涓浘鏍�
+ .setPriority(NotificationCompat.PRIORITY_LOW)
+// .setOngoing(true) // 璁剧疆閫氱煡涓烘寔缁�氱煡锛屼笉鍙竻闄�
+// .setAutoCancel(false) // 鈫� 璁剧疆涓轰笉鍙嚜鍔ㄥ彇娑�
+// .setShowWhen(false) // 涓嶆樉绀烘椂闂�
+// .setOnlyAlertOnce(true) // 鍙彁绀轰竴娆�
+ .build();
+
+ // 鍚姩鍓嶅彴鏈嶅姟
+ startForeground(NOTIFICATION_ID, notification);
+
+ // 濡傛灉鏈嶅姟琚潃姝伙紝涓嶅啀閲嶆柊鍒涘缓锛堥噸寤猴細START_STICKY,涓嶉噸寤猴細START_NOT_STICKY锛�
+ return START_NOT_STICKY;
+ }
+
+ private void createNotificationChannel() {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ NotificationChannel serviceChannel = new NotificationChannel(
+ CHANNEL_ID,
+ getString(R.string.host_process_service_channel),
+ NotificationManager.IMPORTANCE_LOW // 璁剧疆涓轰綆閲嶈鎬э紝涓嶄細鍙戝嚭澹伴煶
+ );
+// serviceChannel.setDescription("鐢ㄤ簬淇濇寔瀹夸富杩涚▼杩愯");
+ NotificationManager manager = getSystemService(NotificationManager.class);
+ manager.createNotificationChannel(serviceChannel);
+ }
+ }
+
+ @Nullable
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ isRunning = false;
+ }
+
+ public static boolean isServiceRunning() {
+ return isRunning;
+ }
+}
+
--
Gitblit v1.8.0