From 7ef6086f504fe92655820ccdb2e48165519144ac Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 23 Apr 2025 12:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=89=8D=E5=8F=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpmaster/FloatingWindow.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/FloatingWindow.java b/mpmaster/src/main/java/com/xypower/mpmaster/FloatingWindow.java index a7a441cc..e6f941c0 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/FloatingWindow.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/FloatingWindow.java @@ -1,9 +1,14 @@ package com.xypower.mpmaster; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Rect; +import android.os.Build; +import android.os.Handler; import android.os.IBinder; import android.text.Editable; import android.text.TextWatcher; @@ -16,8 +21,13 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import androidx.core.app.NotificationCompat; + public class FloatingWindow extends Service { + private static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 8466505; + private static final String FOREGROUND_CHANNEL_ID = "floatingwin_mpmst"; + private Context mContext; private WindowManager mWindowManager; private View mView; @@ -40,6 +50,28 @@ public class FloatingWindow extends Service { allAboutLayout(intent); moveView(); + final Context context = getApplicationContext(); + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + try { + createNotificationChannel(); + + Notification notification = new NotificationCompat.Builder(context, FOREGROUND_CHANNEL_ID) + .setContentTitle("FloatingWindow") + .setContentText("App Running") + // .setSmallIcon(R.drawable.ic_notification) + .setPriority(NotificationCompat.PRIORITY_LOW) + .build(); + + startForeground(NOTIFICATION_ID_FOREGROUND_SERVICE, notification); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }, 4000); + return super.onStartCommand(intent, flags, startId); } @@ -58,6 +90,21 @@ public class FloatingWindow extends Service { super.onDestroy(); } + private void createNotificationChannel() { + // >= Android 8.0 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + FOREGROUND_CHANNEL_ID, + "FloatingWindow", + NotificationManager.IMPORTANCE_LOW); + + NotificationManager manager = getSystemService(NotificationManager.class); + if (manager != null) { + manager.createNotificationChannel(channel); + } + } + } + WindowManager.LayoutParams mWindowsParams; private void moveView() { /*