From 09ddedd6c91092cdb51d68dce78bba6653e0b0c1 Mon Sep 17 00:00:00 2001 From: Hny0305Lin <1553809191@qq.com> Date: Sun, 14 Jul 2024 13:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89Release=201.3=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E8=83=8C=E6=99=AF=E8=BF=90=E8=A1=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=8F=90=E5=8D=87=E5=AE=89=E5=85=A8=E6=80=A7(BETA)=E3=80=81?= =?UTF-8?q?=E4=B8=BB=E5=87=BD=E6=95=B0=E9=80=82=E9=85=8D=E8=83=8C=E6=99=AF?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=9B=B4=E5=A4=9A=E5=8A=9F=E8=83=BD(BETA)?= =?UTF-8?q?=E3=80=81=E8=B0=83=E6=95=B4=E8=83=8C=E6=99=AF=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=9C=BA=E5=88=B6(BETA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fun/ChatService/MyForegroundService.java | 19 +++++++++++---- .../nearlink/nlchat/fun/MainActivity.java | 24 ++++++++++++++----- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatService/MyForegroundService.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatService/MyForegroundService.java index c346fa4..0569760 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatService/MyForegroundService.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatService/MyForegroundService.java @@ -25,9 +25,9 @@ public class MyForegroundService extends Service { private Runnable stopSelfRunnable = new Runnable() { @Override public void run() { - stopSelf(); - showCompletionNotification(); - exitApp(); + stopSelf(); //停止服务 + showCompletionNotification(); //显示完成通知 + exitApp(); //退出应用 } }; private Runnable updateNotificationRunnable; @@ -44,10 +44,10 @@ public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "服务已启动"); createNotificationChannel(); - endTime = System.currentTimeMillis() + 10 * 60 * 1000; // 10分钟后停止服务 + endTime = System.currentTimeMillis() + 10 * 60 * 1000; // 1分钟后停止服务 startForeground(1, createNotification("保持软件后台运行,已启用,请注意电池消耗,消耗过快请关闭本程序。")); - // 10分钟后自动停止服务 + // 1分钟后自动停止服务 handler.postDelayed(stopSelfRunnable, 10 * 60 * 1000); // 每秒更新通知 @@ -58,6 +58,9 @@ public void run() { if (remainingTime > 0) { startForeground(1, createNotification("剩余时间: " + remainingTime / 1000 + "秒")); handler.postDelayed(this, 1000); + } else { + // 当计时结束时停止更新通知 + handler.removeCallbacks(this); } } }; @@ -121,10 +124,16 @@ private void exitApp() { // 关闭所有Activity Intent broadcastIntent = new Intent(); broadcastIntent.setAction("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP"); + broadcastIntent.setPackage("com.haohanyh.linmengjia.nearlink.nlchat.fun"); sendBroadcast(broadcastIntent); // 终止进程 android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } + + public void stopActivities() { + Intent intent = new Intent("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY"); + sendBroadcast(intent); + } } \ No newline at end of file diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java index 0ae3927..b71c942 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java @@ -44,6 +44,7 @@ import androidx.appcompat.widget.AppCompatCheckBox; import androidx.appcompat.widget.AppCompatTextView; import androidx.cardview.widget.CardView; +import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; @@ -172,11 +173,12 @@ public void run() { stopService(new Intent(MainActivity.this, MyForegroundService.class)); } }; - private BroadcastReceiver exitReceiver = new BroadcastReceiver() { + + private BroadcastReceiver finishActivityReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if ("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP".equals(intent.getAction())) { - finish(); + if ("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY".equals(intent.getAction())) { + ActivityCompat.finishAffinity(MainActivity.this); } } }; @@ -185,6 +187,7 @@ public void onReceive(Context context, Intent intent) { * * @param savedInstanceState */ + @SuppressLint({"ObsoleteSdkInt", "InlinedApi"}) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -196,13 +199,15 @@ protected void onCreate(Bundle savedInstanceState) { return insets; }); - IntentFilter filter = new IntentFilter("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP"); + // 根据Android版本注册广播接收器,以确保应用的兼容性,注册监听退出应用的广播接收器 + IntentFilter filter = new IntentFilter("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerReceiver(exitReceiver, filter, Context.RECEIVER_NOT_EXPORTED); + registerReceiver(finishActivityReceiver, filter, Context.RECEIVER_NOT_EXPORTED); } else { - registerReceiver(exitReceiver, filter); + registerReceiver(finishActivityReceiver, filter, Context.RECEIVER_EXPORTED); } + // 设置屏幕常亮 if (MobileKeepScreenOn) {getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);} Init(); } @@ -223,6 +228,13 @@ protected void onStop() { handler.postDelayed(stopServiceRunnable, 10 * 60 * 1000); } + @Override + protected void onDestroy() { + super.onDestroy(); + // 注销广播接收器 + unregisterReceiver(finishActivityReceiver); + } + /** * Init()为初始化软件控件、权限等内容,启动接下来的可使用的控件 * diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d596da1..2ec9c03 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,7 @@ NLChat com.haohanyh.linmengjia.nearlink.nlchat.fun - 1.3.249.2024.0713 + 1.3.252.2024.0714 NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。 友情感谢