67 lines
2.1 KiB
Plaintext
Executable File
67 lines
2.1 KiB
Plaintext
Executable File
package de.eclipsemagazin.mqtt.push;
|
|
|
|
import android.app.Service;
|
|
import android.content.Intent;
|
|
import android.os.IBinder;
|
|
import android.widget.Toast;
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
|
|
|
|
|
|
public class MQTTService extends Service {
|
|
|
|
|
|
public static final String BROKER_URL = "tcp://222.74.233.2:1883";
|
|
|
|
public static final String clientId = "android-client";
|
|
|
|
public static final String TOPIC = "warnings";
|
|
|
|
public static final String USERNAME = "cmkj";
|
|
|
|
public static final String PASSWORD = "cmkj";
|
|
|
|
private MqttClient mqttClient;
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
|
@Override
|
|
public void onStart(Intent intent, int startId) {
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
super.run();
|
|
try {
|
|
mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
|
|
mqttClient.setCallback(new PushCallback(MQTTService.this));
|
|
MqttConnectOptions ss = new MqttConnectOptions();
|
|
ss.setUserName(USERNAME);
|
|
ss.setPassword(PASSWORD.toCharArray());
|
|
mqttClient.connect(ss);
|
|
mqttClient.subscribe(TOPIC);
|
|
} catch (MqttException e) {
|
|
// Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}.start();
|
|
|
|
super.onStart(intent, startId);
|
|
}
|
|
@Override
|
|
public void onDestroy() {
|
|
try {
|
|
mqttClient.disconnect(0);
|
|
} catch (MqttException e) {
|
|
// Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|