Android14+MQTT

上一篇博文中说到了如何升级MQTT使其支持Android12,但是在实际运行的过程中发现,在Android14手机上会出现闪退现象。

通过搜索发现MQTT已经升级,现已支持Android X和Android14,仓库地址:https://github.com/mayzs/paho.mqtt.android?tab=readme-ov-file

接下来介绍如何在现有项目的基础上升级配置使其支持Android14.

我使用的项目配置:

SDK:34,最高支持Android14
Android Gradle Plugin Version: 8.4.0
Gradle Version: 8.6
Min SDK Version: 28
JavaVersion:17

 支持Android14的MQTT的官方教程

修改原有 eclipse paho mqtt android 项目,支持AndroidX,支持 Android 14

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'io.github.mayzs:paho.mqtt.android:1.1.7'
低版本 gradle 使用 implementation 'io.github.mayzs:paho.mqtt.android:1.1.7@aar'

manifest中 service 标签添加  android:foregroundServiceType="dataSync"

MqttAndroidClient client = new MqttAndroidClient(context, uri, clientId);
  android 8.0 及以上添加
 client.setForegroundService(notification,1));

 按照以上步骤:

1. 在build.gradle(以project视图查看文件,app目录下的build.gradle)中添加以下内容

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'io.github.mayzs:paho.mqtt.android:1.1.7'

 2. 在AndroidManifest.xml中的service标签添加android:foregroundServiceType="dataSync"并添加相应的权限,Android14对于权限的要求更加严格,如果不添加以下权限,在连接MQTT时会出错,APP出现闪退现象。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

 在service标签中都添加enable, exported, foregroundServiceType,如下所示

 <service
     android:name=".MqttService"
     android:enabled="true"
     android:exported="true"
     android:foregroundServiceType="dataSync"/>
<service
     android:name="org.eclipse.paho.android.service.MqttService"
     android:enabled="true"
     android:exported="true"
     android:foregroundServiceType="dataSync"/>

3. 需要注意client.setForegroundService(notification,1));中的notification需要自己手动添加,这并不属于某个库或类。

如下,新建NotificationHelper类:

package com.wang.mqtt;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;

public class NotificationHelper {
    public static final String CHANNEL_ID = "MqttServiceChannel";
    private Context context;

    public NotificationHelper(Context context) {
        this.context = context;
        createNotificationChannel();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Mqtt Service Channel";
            String description = "Channel for Mqtt Service";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public Notification createNotification() {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        return new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("Mqtt Service")
                .setContentText("Mqtt Service is running")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
    }
}

在MqttService的init函数中添加以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationHelper notificationHelper = new NotificationHelper(this);
            Notification notification = notificationHelper.createNotification();
            mqttAndroidClient.setForegroundService(notification, 1);
        }

 也就是

Android14下添加最新版MQTT支持的操作不算复杂,值得注意的是,NotificationHelper需要自己新建。Android14的权限要求更加严格,如果出现权限不足则添加相应的权限。

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统根据B/S,即所谓的电脑浏览器/网络服务器方式,运用Java技术性,挑选MySQL作为后台系统。系统主要包含对客服聊天管理、字典表管理、公告信息管理、金融工具管理、金融工具收藏管理、金融工具银行卡管理、借款管理、理财产品管理、理财产品收藏管理、理财产品银行卡管理、理财银行卡信息管理、银行卡管理、存款管理、银行卡记录管理、取款管理、转账管理、用户管理、员工管理等功能模块。 文中重点介绍了银行管理的专业技术发展背景和发展状况,随后遵照软件传统式研发流程,最先挑选适用思维和语言软件开发平台,依据需求分析报告模块和设计数据库结构,再根据系统功能模块的设计制作系统功能模块图、流程表和E-R图。随后设计架构以及编写代码,并实现系统能模块。最终基本完成系统检测和功能测试。结果显示,该系统能够实现所需要的作用,工作状态没有明显缺陷。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。进入银行卡列表,管理员可以进行查看列表、模糊搜索以及相关维护等操作。用户进入系统可以查看公告和模糊搜索公告信息、也可以进行公告维护操作。理财产品管理页面,管理员可以进行查看列表、模糊搜索以及相关维护等操作。产品类型管理页面,此页面提供给管理员的功能有:新增产品类型,修改产品类型,删除产品类型。
好的,以下是Android MQTT连接MQTT服务器并获取传感器数据的示例代码: 1. 添加依赖 在项目的 build.gradle 文件中添加以下依赖: ``` dependencies { implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' } ``` 2. 连接MQTT服务器 在需要连接MQTT服务器的Activity或者Fragment中,添加以下代码: ``` String clientId = MqttClient.generateClientId(); MqttConnectOptions options = new MqttConnectOptions(); options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1); options.setCleanSession(true); try { IMqttClient mqttClient = new MqttClient("tcp://your.mqtt.server:1883", clientId, new MemoryPersistence()); mqttClient.connect(options); // 订阅主题 mqttClient.subscribe("temperature"); mqttClient.subscribe("humidity"); mqttClient.subscribe("sensor_status"); mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) { // 处理连接断开异常 } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { // 处理接收到的传感器数据 if ("temperature".equals(topic)){ String temperature = new String(message.getPayload()); // 处理温度数据 } else if ("humidity".equals(topic)) { String humidity = new String(message.getPayload()); // 处理湿度数据 } else if ("sensor_status".equals(topic)) { String status = new String(message.getPayload()); // 处理传感器状态数据 } } @Override public void deliveryComplete(IMqttDeliveryToken token) { // 消息发布成功 } }); } catch (MqttException e) { e.printStackTrace(); } ``` 代码中,首先生成客户端ID,并创建MqttConnectOptions对象配置连接选项,然后创建MqttClient对象连接至MQTT服务器,订阅三个主题:temperature、humidity、sensor_status,设置回调接收传感器数据。当服务器有新数据时,会调用回调接口的messageArrived方法进行处理。 3. 发送传感器数据 客户端向MQTT服务器发送数据,可以使用以下代码: ``` String topic = "temperature"; String payload = "24.3"; IMqttMessage message = new MqttMessage(payload.getBytes()); try { mqttClient.publish(topic, message); } catch (MqttException e) { e.printStackTrace(); } ``` 以上代码可以在需要发送传感器数据的地方进行调用,将温度数据24.3发布至"temperature"主题,实际中需要根据实际传感器数据进行修改。 希望我的回答能够解决你的问题!如果还有其他问题,欢迎继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值