当前位置: 首页 > news >正文

Android前台服务和通知

前台服务

Android 13及以上系统需要动态获取通知权限。

//android 13及以上系统动态获取通知权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();
}
private void checkPostNotificationPermission() {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 200);} else if (manager != null) {//通知ID设置(您可以使用任何您想要的ID,相同ID通知只会显示一个)。manager.notify(2, builder.build());}
}@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == 200) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//允许了通知权限} else {Toast.makeText(this, "您拒绝了通知权限", Toast.LENGTH_SHORT).show();}}
}

别忘记添加通知权限

<!--发布通知权限-->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

开启前台服务需要添加前台服务权限

<!--前台服务权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

通知震动需要添加震动权限

<!--振动器权限-->
<uses-permission android:name="android.permission.VIBRATE" />

前台服务代码

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;import com.example.javatest.MainActivity;
import com.example.javatest.R;/*** created by cwj on 2023-10-19* Description: 前台服务*/
public class ForegroundService extends Service {@Overridepublic void onCreate() {super.onCreate();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {CharSequence name = getString(R.string.app_name);int importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance);channel.setDescription("前台服务守护进程");NotificationManager notificationManager = getSystemService(NotificationManager.class);notificationManager.createNotificationChannel(channel);}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID")// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(getString(R.string.app_name))//设置通知的内容.setContentText("进程守护中")//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent);// 将通知设置为前台服务startForeground(1, builder.build());}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 在这里执行需要在前台运行的任务// 返回START_STICKY表示服务在被杀死后会自动重启return START_STICKY;}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();// 停止前台服务stopForeground(true);stopSelf();}
}

application中添加服务

<serviceandroid:name=".service.ForegroundService"android:enabled="true"android:exported="false" />

activity中启动服务

Intent intent = new Intent(this, ForegroundService.class);
// Android 8.0使用startForegroundService在前台启动新服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(intent);
} else {startService(intent);

创建通知

 模拟发送通知

binding.iv.setOnClickListener(v ->showNotification(getString(R.string.app_name), dateToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "您收到一条消息")
);

创建通知及通知点击取消通知

private NotificationCompat.Builder builder;
private NotificationManagerCompat manager;private void showNotification(String title, String content) {// 创建通知频道,如果用户没有创建,则会自动创建。String CHANNEL_ID = "message";if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (getSystemService(NotificationManager.class).getNotificationChannel(CHANNEL_ID) == null) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "消息", NotificationManager.IMPORTANCE_DEFAULT);//通知的振动模式。channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});//为通知通道启用振动。channel.enableVibration(true);//设置通道的颜色。channel.setLightColor(Color.RED);//设置通道的锁屏可见性。channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);getSystemService(NotificationManager.class).createNotificationChannel(channel);}}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);// 创建并分发通知。builder = new NotificationCompat.Builder(this, CHANNEL_ID)// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(title)//设置通知的内容.setContentText(content)//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent)//开启点按通知后自动移除通知.setAutoCancel(true);manager = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();} else {// 在 Android 10 或更早版本中,不需要请求此权限。//ID要和前台服务ID区分开,相同ID只能显示一条通知。manager.notify(2, builder.build());}

activity完整代码

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;import android.Manifest;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;import com.example.javatest.databinding.ActivityMainBinding;
import com.example.javatest.service.ForegroundService;import java.text.SimpleDateFormat;
import java.util.Date;public class MainActivity extends AppCompatActivity {private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());initView();initData();}private void initView() {//android 13及以上系统动态获取通知权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();}Intent intent = new Intent(this, ForegroundService.class);// Android 8.0使用startForegroundService在前台启动新服务if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(intent);} else {startService(intent);}}private void initData() {binding.iv.setOnClickListener(v ->showNotification(getString(R.string.app_name), dateToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "您收到一条消息"));}private String dateToString(long time, String format) {Date date = new Date(time);SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);return simpleDateFormat.format(date);}private NotificationCompat.Builder builder;private NotificationManagerCompat manager;private void showNotification(String title, String content) {// 创建通知频道,如果用户没有创建,则会自动创建。String CHANNEL_ID = "message";if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (getSystemService(NotificationManager.class).getNotificationChannel(CHANNEL_ID) == null) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "消息", NotificationManager.IMPORTANCE_DEFAULT);//通知的振动模式。channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});//为通知通道启用振动。channel.enableVibration(true);//设置通道的颜色。channel.setLightColor(Color.RED);//设置通道的锁屏可见性。channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);getSystemService(NotificationManager.class).createNotificationChannel(channel);}}//设置服务跳转Intent intent = new Intent(this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);// 创建并分发通知。builder = new NotificationCompat.Builder(this, CHANNEL_ID)// 设置通知的小图标。.setSmallIcon(R.mipmap.ic_launcher)//设置通知的标题。.setContentTitle(title)//设置通知的内容.setContentText(content)//设置通知的优先级。.setPriority(NotificationCompat.PRIORITY_DEFAULT)//震动模式的通知在Android O及以上。.setVibrate(new long[]{0, 1000, 500, 1000})//在Android O及以上的系统上使用LED光色和微信号。.setLights(Color.RED, 1000, 1000)//设置Android O及以上版本的通知声音。.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/raw/notification_sound"))//如果需要,在Android O及以上版本设置较大的通知标题。// .setLargeTitle("Large Title Text")//如果需要,在Android O及以上版本设置通知的子文本。// .setSubText("Sub Text")//如果需要,在Android O及以上版本设置大字体通知。// .setStyle(new NotificationCompat.BigTextStyle().bigText("Big Text"))//如果需要,在Android O及以上版本设置通知摘要文本(当有多个具有相同优先级的通知时显示摘要文本)。// .setSummaryText("Summary Text")//如果需要,在通知中添加动作按钮(可用于启动活动或发送广播)。// .addAction(R.drawable.ic_action, "Action Title", PendingIntent)//设置可点击跳转.setContentIntent(pendingIntent)//开启点按通知后自动移除通知.setAutoCancel(true);manager = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {checkPostNotificationPermission();} else {// 在 Android 10 或更早版本中,不需要请求此权限。//显示ID为1的通知(您可以使用任何您想要的ID)。manager.notify(2, builder.build());}}private void checkPostNotificationPermission() {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 200);} else if (manager != null) {//显示ID为1的通知(您可以使用任何您想要的ID)。manager.notify(2, builder.build());}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == 200) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//允许了通知权限} else {Toast.makeText(this, "您拒绝了通知权限", Toast.LENGTH_SHORT).show();}}}
}

完整配置文件AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><!--网络权限--><uses-permission android:name="android.permission.INTERNET" /><!--前台服务权限--><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><!--振动器权限--><uses-permission android:name="android.permission.VIBRATE" /><!--全屏意图权限--><uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /><!--发布通知权限--><uses-permission android:name="android.permission.POST_NOTIFICATIONS" /><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.JavaTest"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><serviceandroid:name=".service.ForegroundService"android:enabled="true"android:exported="false" /></application></manifest>

build.gradle.kts文件中添加viewBinding支持

buildFeatures{viewBinding = true
}

相关文章:

Android前台服务和通知

前台服务 Android 13及以上系统需要动态获取通知权限。 //android 13及以上系统动态获取通知权限 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {checkPostNotificationPermission(); } private void checkPostNotificationPermission() {if (ActivityCompat.chec…...

算法进修Day-36

算法进修Day-36 71. 简化路径 难度&#xff1a;中等 题目要求&#xff1a; 给你一个字符串 path &#xff0c;表示指向某一文件或目录的 Unix 风格 绝对路径 &#xff08;以 / 开头&#xff09;&#xff0c;请你将其转化为更加简洁的规范路径。 在 Unix 风格的文件系统中&am…...

postman自动化运行接口测试用例

做过接口测试的人&#xff0c;应该都知道postman &#xff0c;我们在日常的时候都可以利用postman做接口测试&#xff0c;我们可以把接口的case保存下来在collection里面&#xff0c;那么可能会有这样的需求&#xff0c;我们怎么把collection的用例放到jenkins中定时执行呢&…...

【Linux】Linux环境搭建

Linux环境搭建 前言Linux 环境的搭建方式一、Linux环境搭载购买云服务器 二、 使用 XShell 远程登陆到 Linux关于 Linux 桌面下载安装 XShell查看 Linux 主机 ip使用 XShell 登陆主机XShell 下的复制粘贴 前言 Linux 环境的搭建方式 主要有三种 直接安装在 物理机 上. 但是由…...

k8s day07

昨日内容回顾: - 污点: 影响Pod调度&#xff0c;污点是作用在worker节点上。语法格式: KEY[VALUE]:EFFECT 有三种污点。 EFFECT: - NoSchedule&#xff1a; 已经调度到当前节点的Pod不驱逐&#xff0c;并且不在接…...

压力大,休息日都没有,更别说年休假了

我一周要工作六天&#xff0c;每天至少十小时&#xff0c;连个休息日都没有。哪来的年休假&#xff1f;...

人手一个助理,三句话让AI替我们上班

目录 前言 从大模型上长出来的 AI 原生应用&#xff0c;才是关键 而这看起来只是一个小小的办公沟通场景&#xff0c;却是大模型重构的一个非常典型的场景。背后考验的也是大模型的综合能力应用 这种从AI原生角度进行的重构&#xff0c;离不开大模型的理解、生成、逻辑、记…...

【Eclipse Maven Tycho】如何通过maven执行eclipse application

通过maven执行eclipse application 前言命令行下运行通过maven tycho运行 前言 eclipse其实不只是一个桌面&#xff08;GUI&#xff09;程序&#xff0c;他还可以是一个命令行程序。如果你的产品或软件是基于eclipse开发的&#xff0c;并且他没有UI相关的功能&#xff0c;那么…...

(一)docker:建立oracle数据库

前言&#xff0c;整个安装过程主要根据docker-images/OracleDatabase/SingleInstance /README.md &#xff0c;里边对如何制作容器讲的比较清楚&#xff0c;唯一问题就是都是英文&#xff0c;可以使用谷歌浏览器自动翻译成中文&#xff0c;自己再对照英文相互参照来制作提前准备…...

在配置文件“tsconfig.json”中找不到任何输入。指定的 “include“ 路径为“[“**/*“]”,“exclude“ 路径为[]

在vscode中项目下的tsconfig.json莫名报错 解决办法 在目录中随便创建一个后缀为.ts的文件 便不再报错...

java编译时指定classpath

说明 Java编译时可以通过选项--class-path <path>&#xff0c;或者 -classpath <path>&#xff0c;或者-cp <path>来指定查找用户类文件、注释程序处理程序、或者源文件的位置。这个设置覆盖CLASSPATH环境变量的设置。如果没有设置-sourcepath&#xff0c;那…...

分享一下抽奖活动小程序怎么做

在当今数字化时代&#xff0c;抽奖活动小程序已成为一种高效、创新的营销方式。它不仅能够吸引用户的注意力&#xff0c;提高品牌知名度&#xff0c;还能促进用户参与度&#xff0c;增强用户对品牌的忠诚度。本文将详细介绍如何制作一个成功的抽奖活动小程序&#xff0c;以及它…...

同为科技(TOWE)工业级多位USB快充桌面PDU插座

如今&#xff0c;许多便捷式、轻薄化电子设备越来越多&#xff0c;很多设备上预留的端口越来越少&#xff0c;甚至很多款笔记本电脑只预留了一个单一的Type-C接口。这样做虽然在体验感、美观度和轻薄尺寸的优势显而易见&#xff0c;然而也存在接口不足的明显弊端。USB快充插排产…...

testt

wd wwwwwwwwwwwwww qdwqdwqd...

Git Bash(一)Windows下安装及使用

目录 一、简介1.1 什么是Git&#xff1f;1.2 Git 的主要特点1.3 什么是 Git Bash&#xff1f; 二、下载三、安装3.1 同意协议3.2 选择安装位置3.3 其他配置&#xff08;【Next】 即可&#xff09;3.4 安装完毕3.5 打开 Git Bash 官网地址&#xff1a; https://www.git-scm.com/…...

软考公告 | 关于2023年下半年软考批次安排

按照《2023年下半年计算机技术与软件专业技术资格&#xff08;水平&#xff09;考试有关工作调整的通告》&#xff0c;自2023年下半年起&#xff0c;计算机软件资格考试方式均由纸笔考试改革为计算机化考试。 为进一步提升考试服务质量和水平, 便利考生考试, 减少考生参加考试…...

react 中ref 属性的三种写法

目录 1. 字符串 ref 2.dom节点上使用回调函数ref 3.React.createRef() 1. 字符串 ref 最早的ref用法。&#xff08;由于效率问题&#xff0c;现在官方不推荐使用这种写法。&#xff09; 1.dom节点上使用&#xff0c;通过this.refs.xxxx来引用真实的dom节点 <input ref&q…...

v4l2-ioctl.c的一些学习和整理

可以发现&#xff0c;这个宏用的很好&#xff0c;简洁易扩展&#xff0c;自己写代码可以学习下 #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \[_IOC_NR(_ioctl)] { \.ioctl _ioctl, \.flags _flags, \.name #_ioctl, \.func _func, \.debug _…...

Python实战小项目分享

Python实战小项目包括网络爬虫、数据分析和可视化、文本处理、图像处理、聊天机器人、任务管理工具、游戏开发和网络服务器等。这些项目提供了实际应用场景和问题解决思路&#xff0c;可以选择感兴趣的项目进行实践&#xff0c;加深对Python编程的理解和掌握。在实践过程中&…...

使用Dockerfile生成docker镜像和容器的方法记录

一、相关介绍 Docker 是一个开源的容器化平台&#xff0c;其中的主要概念是容器和镜像。 容器是 Docker 的运行实例。 它是一个独立并可执行的软件包&#xff0c;包含了应用程序及其依赖的所有组件&#xff08;如代码、运行时环境、系统工具、库文件等&#xff09;。容器可以在…...

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

conda相比python好处

Conda 作为 Python 的环境和包管理工具&#xff0c;相比原生 Python 生态&#xff08;如 pip 虚拟环境&#xff09;有许多独特优势&#xff0c;尤其在多项目管理、依赖处理和跨平台兼容性等方面表现更优。以下是 Conda 的核心好处&#xff1a; 一、一站式环境管理&#xff1a…...

日语AI面试高效通关秘籍:专业解读与青柚面试智能助攻

在如今就业市场竞争日益激烈的背景下&#xff0c;越来越多的求职者将目光投向了日本及中日双语岗位。但是&#xff0c;一场日语面试往往让许多人感到步履维艰。你是否也曾因为面试官抛出的“刁钻问题”而心生畏惧&#xff1f;面对生疏的日语交流环境&#xff0c;即便提前恶补了…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

SpringCloudGateway 自定义局部过滤器

场景&#xff1a; 将所有请求转化为同一路径请求&#xff08;方便穿网配置&#xff09;在请求头内标识原来路径&#xff0c;然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...

全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比

目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec&#xff1f; IPsec VPN 5.1 IPsec传输模式&#xff08;Transport Mode&#xff09; 5.2 IPsec隧道模式&#xff08;Tunne…...

return this;返回的是谁

一个审批系统的示例来演示责任链模式的实现。假设公司需要处理不同金额的采购申请&#xff0c;不同级别的经理有不同的审批权限&#xff1a; // 抽象处理者&#xff1a;审批者 abstract class Approver {protected Approver successor; // 下一个处理者// 设置下一个处理者pub…...

怎么让Comfyui导出的图像不包含工作流信息,

为了数据安全&#xff0c;让Comfyui导出的图像不包含工作流信息&#xff0c;导出的图像就不会拖到comfyui中加载出来工作流。 ComfyUI的目录下node.py 直接移除 pnginfo&#xff08;推荐&#xff09;​​ 在 save_images 方法中&#xff0c;​​删除或注释掉所有与 metadata …...

STM32---外部32.768K晶振(LSE)无法起振问题

晶振是否起振主要就检查两个1、晶振与MCU是否兼容&#xff1b;2、晶振的负载电容是否匹配 目录 一、判断晶振与MCU是否兼容 二、判断负载电容是否匹配 1. 晶振负载电容&#xff08;CL&#xff09;与匹配电容&#xff08;CL1、CL2&#xff09;的关系 2. 如何选择 CL1 和 CL…...