上一篇博文中说到了如何升级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的权限要求更加严格,如果出现权限不足则添加相应的权限。