eclipse.paho mqtt适配安卓14
问题描述:Android paho mqtt crashes Android 12 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE
解决方法:
-
打开 org.eclipse.paho:org.eclipse.paho.android.service:1.1.1,下载zip,解压
-
将org及以下所有文件拷贝到Android目录下
-
将gradle中引用的
org.eclipse.paho:org.eclipse.paho.android.service:1.1.1
注释掉,其他保留
-
打开如图两个文件,将原来的引用:
import android.support.v4.content.LocalBroadcastManager;
改成import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-
其中,
MqttAndroidClient
还需要实现三接口,鼠标悬浮在爆红上,选自动生成即可 -
修改该文件
// 安卓12及其以上需要适配
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
action), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
action), PendingIntent.FLAG_UPDATE_CURRENT);
}
- 结束