uniapp system broadcast, custom broadcast plug-in Ba-Broadcast

Introduction ( download address )

Ba-Broadcast is a uniapp plug-in that can monitor system broadcasts, such as screen off, screen on, sound, battery, etc. Support monitoring system broadcasts; support custom broadcasts; support sending broadcasts.

  • Support system broadcast (support all broadcast configurations supported by the device, and support the return of system parameters in the same way)
  • Support custom broadcasting (parameters can be customized at will, multiple)
  • Support for sending broadcasts
  • If special, please contact the author to customize

screenshot display

insert image description here

Instructions

quote

scriptIntroduce components in

	const broadcast = uni.requireNativePlugin('Ba-Broadcast')

example

It can be called scripton (example reference, you can modify it according to your own business and calling method)

<script>
	const broadcast = uni.requireNativePlugin('Ba-Broadcast')
	export default {
    
    
		data() {
    
    
			return {
    
    
				msgList: [],
				actions: [
					'android.intent.action.SCREEN_OFF', //关闭屏幕
					'android.intent.action.SCREEN_ON', //打开屏幕
					'android.intent.action.USER_PRESENT', //解锁屏幕
					'com.ba.temp.action', //自定义广播
				],
				action: 'com.ba.temp.action',
				msg: '测试参数'
			}
		},
		methods: {
    
    
			register() {
    
     //注册监听
				broadcast.register({
    
    
					actions: this.actions
				}, res => {
    
    
					console.log(res);
					let msg = '';
					if (res.msg) {
    
    
						msg = res.msg;
					}
					if (res.action) {
    
     //判断是否是接收到广播消息
						this.msgList.push(res.action);
						msg = res.action;
					} else if (res.ok) {
    
    
	
					}
					uni.showToast({
    
    
						title: msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			unregister() {
    
     //注销监听
				broadcast.unregister({
    
    }, res => {
    
    
					console.log(res);
					uni.showToast({
    
    
						title: res.msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			sendBroadcast() {
    
     //发送自定义广播
				if (!this.action) {
    
    
					uni.showToast({
    
    
						title: 'action 不能为空',
						icon: "none"
					})
					return
				}
				broadcast.sendBroadcast({
    
    
					action: this.action, //广播 action
					msg: this.msg, //随意自定义参数,可多个
				}, res => {
    
    
					console.log(res);
					uni.showToast({
    
    
						title: res.msg,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>

System Broadcast List

Here is a list of some commonly used system broadcasts, all broadcasts of the system can be supported, and the parameters returned are consistent with the system

Common broadcast list
action illustrate parameter name illustrate
android.intent.action.SCREEN_OFF turn off the screen
android.intent.action.SCREEN_ON open screen
android.intent.action.USER_PRESENT unlock screen
android.intent.action.REBOOT Reboot the device
android.intent.action.ACTION_SHUTDOWN shutdown system
android.intent.action.CLOSE_SYSTEM_DIALOGS When the user clicks the home button (homekey)
and the multitasking button (recentapps) (returned reason values ​​will be different)
, when the screen times out to lock the screen, when the user presses the power button to lock the screen
android.intent.action.ACTION_POWER_CONNECTED Broadcast when external power is plugged in
android.intent.action.ACTION_POWER_DISCONNECTED Broadcast when the external power link has been disconnected
android.intent.action.BATTERY_CHANGED State of charge, or change in battery capacity
android.intent.action.BATTERY_LOW battery is low
android.intent.action.BATTERY_OKAY The battery is full, i.e. broadcasts when the battery changes from low to full
android.intent.action.AIRPLANE_MODE Broadcast when airplane mode is turned off or on state 1: open 0: close
android.intent.action.HEADSET_PLUG Broadcast when headphones are plugged into the headphone port state 1: plug in 0: pull out
android.provider.Telephony.SMS_RECEIVE Receiving text messages requires the corresponding permission android.permission.RECEIVE_SMS
android.media.VOLUME_CHANGED_ACTION volume change
android.intent.action.TIME_TICK The current time changes (the normal passage of time). The current time changes, sent every minute
android.intent.action.PACKAGE_INSTALL Trigger a broadcast when a download is complete and the installation is complete, like downloading an app in the electronic market
android.intent.action.PACKAGE_REMOVED The broadcast sent after a certain APK is successfully deleted (the package program being installed cannot receive this broadcast)
android.intent.action.PACKAGE_REPLACED The broadcast sent when replacing an existing installation package (this broadcast will be sent regardless of whether the currently installed APP is newer or older than the previous one)
android.intent.action.PACKAGE_ADDED After successfully installing the APK
android.intent.action.PACKAGE_CHANGED An existing application package has changed, including the package name
android.intent.action.PACKAGE_DATA_CLEARED The broadcast sent when clearing the data of an application (in Settings - Application Management - select an application, and then click Clear Data?)

method list

name illustrate
register register listener
unregister logout listener
sendBroadcast Send a custom broadcast

register method parameter

Register to listen to the broadcast

attribute name type required Defaults illustrate
actions Array true ‘’ The broadcast that needs to be monitored is required. You can refer to "System Broadcast" in this article, or you can configure broadcasts supported by other systems by yourself, or customize broadcasts
The parameters of the received broadcast message
attribute name type illustrate
action String broadcast action
property-name(...) All other parameters returned by the system, such as WiFi status state

sendBroadcast method parameters

Send a custom broadcast

attribute name type required Defaults illustrate
action String true ‘’ broadcast action
attribute name String false ‘’ Custom parameters, the name can be freely defined

Series plug-in

Image selection plugin Ba-MediaPicker ( documentation )

Image editing plugin Ba-ImageEditor ( documentation )

File picker plugin Ba-FilePicker ( documentation )

Application message notification plug-in (multiple styles, new support for resident notification mode) Ba-Notify ( documentation )

Apply the unread badge plugin Ba-Shortcut-Badge ( documentation )

Applying the Ba-Autoboot plugin ( documentation )

Scan code native plug-in (millisecond level, support multi-code) Ba-Scanner-G ( documentation )

扫码原生插件 - 新(可任意自定义界面版本;支持连续扫码;支持设置扫码格式)Ba-Scanner文档

动态修改状态栏、导航栏背景色、字体颜色插件 Ba-AppBar文档

原生sqlite本地数据库管理 Ba-Sqlite文档

安卓保活插件(采用多种主流技术) Ba-KeepAlive文档

安卓快捷方式(桌面长按app图标) Ba-Shortcut文档

自定义图片水印(任意位置) Ba-Watermark文档

最接近微信的图片压缩插件 Ba-ImageCompressor文档

视频压缩、视频剪辑插件 Ba-VideoCompressor文档

动态切换应用图标、名称(如新年、国庆等) Ba-ChangeIcon文档

原生Toast弹窗提示(穿透所有界面、穿透原生;自定义颜色、图标 ) Ba-Toast文档

Image graffiti, brush Ba-ImagePaint ( documentation )

pdf reading (gesture zoom, display page number) Ba-Pdf ( documentation )

Sound reminder, vibration reminder, voice broadcast Ba-Beep ( documentation )

Websocket native service (automatic reconnection, heartbeat detection) Ba-Websocket ( documentation )

SMS monitoring (verification code) Ba-Sms ( documentation )

Smart Install (Automatic Upgrade) Ba-SmartUpgrade ( documentation )

Monitor system broadcast, custom broadcast Ba-Broadcast ( documentation )

Guess you like

Origin blog.csdn.net/u013164293/article/details/128107656