• 大小: 24KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: Java
  • 标签: mqtt  android  activemq  

资源简介

mqtt android使用源码 链接ActiveMQ服务器,其中的代码直接复制在你的项目中就行,记得把包名都改一下,不然会报错,还有你要下载mqttclient的jar包并引用在项目中不然导入的client包会找不到

资源截图

代码片段和文件信息

package com.youngch.common.myapplication.mqtt;


import android.content.Context;

import com.youngch.common.myapplication.mqtt.service.MqttActionListener;
import com.youngch.common.myapplication.mqtt.service.MqttAndroidClient;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;


/**
 * Created by ZhangHs on 2018/4/19.
 * 对mqtt的封装
 */

public class MqttFactory {
    private final Context context;
    private final String serverIP;
    private final String port;
    private final boolean autoConnect;
    private final int connectionTimeout;
    private final int keepAliveInterval;
    private final String clientId;
    private final MqttCallback callback;

    private MqttAndroidClient client;
    private MqttConnectOptions options;
    private MqttActionListener actionListener;

    public MqttFactory(Builder builder) {
        this.context = builder.context;
        this.serverIP = builder.serverIP;
        this.autoConnect = builder.autoConnect;
        this.connectionTimeout = builder.connectionTimeout;
        this.keepAliveInterval =builder. keepAliveInterval;
        this.clientId = builder.clientId;
        this.callback=builder.callback;
        this.port=builder.port;
        init();
    }
    private void init(){
        client=new MqttAndroidClient(context“tcp://“+serverIP+“:“+portclientId);
        client.setCallback(callback);
        options=new MqttConnectOptions();
        options.setAutomaticReconnect(autoConnect);
        options.setConnectionTimeout(connectionTimeout);
        options.setKeepAliveInterval(keepAliveInterval);

    }

    public void connect(){
        try {
            client.connect(options null actionListener=new MqttActionListener(MqttActionListener.TYPE.CONNECTMQTT));
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }


    public static class Builder {
        private Context context;
        private String serverIP;
        private String port;
        private boolean autoConnect;
        private int connectionTimeout;
        private int keepAliveInterval;
        private String clientId;
        private MqttCallback callback;

        public Builder(Context context) {
            this.context = context;
        }
        public Builder port(String port) {
            this.port = port;
            return this;
        }
        public Builder serverIP(String serverIP) {
            this.serverIP = serverIP;
            return this;
        }
        public Builder clientId(String clientId) {
            this.clientId = clientId;
            return this;
        }
        public Builder keepAliveInterval(int keepAliveInterval) {
            this.keepAliveInterval = keepAliveInterval;
            return this;
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-04-24 13:23  mqtt\
     文件        4657  2018-04-20 15:27  mqtt\MqttFactory.java
     文件        1887  2018-04-20 15:27  mqtt\MqttIn.java
     目录           0  2018-04-20 15:29  mqtt\service\
     文件        5083  2018-04-20 14:17  mqtt\service\AlarmPingSender.java
     文件         521  2018-04-20 14:17  mqtt\service\BuildConfig.java
     文件       10895  2018-04-20 15:29  mqtt\service\DatabaseMessageStore.java
     文件         672  2018-04-20 15:29  mqtt\service\MessageStore.java
     文件        1846  2018-04-20 15:27  mqtt\service\MqttActionListener.java
     文件       26588  2018-04-20 15:27  mqtt\service\MqttAndroidClient.java
     文件        1242  2018-04-20 15:27  mqtt\service\MqttCallBackListener.java
     文件       34493  2018-04-20 15:29  mqtt\service\MqttConnection.java
     文件         997  2018-04-20 13:24  mqtt\service\MqttDeliveryTokenAndroid.java
     文件       15375  2018-04-20 15:29  mqtt\service\MqttService.java
     文件         634  2018-04-20 13:24  mqtt\service\MqttServiceBinder.java
     文件        2365  2018-04-20 13:24  mqtt\service\MqttServiceConstants.java
     文件        4742  2018-04-20 13:24  mqtt\service\MqttTokenAndroid.java
     文件         315  2018-04-20 13:24  mqtt\service\MqttTraceHandler.java
     文件        1703  2018-04-20 13:24  mqtt\service\ParcelableMqttMessage.java
     文件         196  2018-04-20 13:24  mqtt\service\Status.java

评论

共有 条评论