DMS ANDROID 教程 hello world

设置androdManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<service android:name="org.eclipse.paho.android.service.MqttService" >
    </service>

导入

import com.aodianyun.dms.android.DMS;
import org.eclipse.paho.client.mqttv3.*;

初始化DMS

DMS.init(getApplicationContext(), "demo", "demo", new MqttCallback(){
     @Override
    public void connectionLost(Throwable throwable) {
        Log.i("DMS","LOSTED");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        Log.i("DMS","RECV  TOPIC: "+s+ " MESSAGE :"+new String( mqttMessage.getPayload()) );
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
});

连接到DMS 并 关注 推送消息

try{
    final String topic = "test";
    DMS.connect(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken iMqttToken) {
            Log.i("DMS","connect success ");
            try{
                DMS.subscribe(topic,new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken iMqttToken) {
                        Log.i("DMS","subscribe success ");
                    }
                    @Override
                    public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                        Log.i("DMS","subscribe failure ");
                    }
                });
                DMS.publish(topic, "hello,world".getBytes(), new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                         Log.i("DMS","publish success ");
                    }

                    @Override
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                        Log.i("DMS","publish failure ");
                    }
                });
            }catch( MqttException e){
                e.printStackTrace();
            }
        }
        @Override
        public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
            Log.i("DMS","connect failure ");
        }
    });
}catch( MqttException e){
    e.printStackTrace();
}