我正在尝试为Android构建一个MQTT“监听器”应用程序,并使用this sample application作为模型。但是,原始示例会抛出“主要活动上的网络”运行时错误,因此我被迫使用AsyncTask类。
主App作为服务运行:LWMQService.java [此处代码的相关部分]
package org.iothub.lightwatchmq;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.iothub.lightwatchmq.PushCallback;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.provider.Settings.Secure;
public class LWMQService extends Service implements SensorEventListener {
-----
String BROKER_URL = "tcp://test.mosquitto.org:1883";
String TOPIC = "iothub";
String TOPIC2 = "iothub2";
String ClientID ;
private MqttClient client;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
------
ClientID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
try {
client = new MqttClient(BROKER_URL, ClientID, new MemoryPersistence());
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
new mConnect ().execute();
// new mTransmit ().execute();
new mListen().execute();;
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(LogMsgID, "LightWatch Service started");
return START_STICKY;
}
@Override
public void onDestroy() {
-----
return;
}
private class mConnect extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...dummy) {
try {
client.connect();
} catch (MqttException e) {
e.printStackTrace();
Log.d(LogMsgID, "Connect Failed");
System.exit(1);
}
return null;
}
}
public class mListen extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...dummy) {
try {
client.setCallback(new PushCallback(this));
client.subscribe(TOPIC2);
} catch (MqttException e) {
e.printStackTrace();
Log.d(LogMsgID, "Listen Failed");
System.exit(1);
}
return null;
}
}
private class mTransmit extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...dummy) {
--------
}
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
---
}
}
mConnect和mTransmit(发布)类正确编译并正常工作但是mListen(subscribe)类我得到一个编译错误,指出“构造函数PushCallback(LWMQService.mListen)未定义”显示在下一行中类
client.setCallback(new PushCallback(this));
PushCallback类在另一个文件中可用:PushCallback.java
package org.iothub.lightwatchmq;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.util.Log;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.iothub.lightwatchmq.LWMQService.mListen;
public class PushCallback implements MqttCallback {
String LogMsgID = "iothub: ";
private ContextWrapper context;
public PushCallback(ContextWrapper context) {
this.context = context;
Log.d(LogMsgID, "PCB-1");
}
@Override
public void connectionLost(Throwable cause) {
//We should reconnect here
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String Topic, MqttMessage message) throws Exception {
// TODO Auto-generated method stub
final Intent intent = new Intent(context, MainActivity.class);
final PendingIntent activity = PendingIntent.getActivity(context, 0, intent, 0);
String RecdMessage = new String(message.getPayload());
Log.d(LogMsgID, RecdMessage);
}
}
我承认我不是很擅长Java而且可能会犯一些明显的错误,但如果有人能帮我解决这个错误,我将不胜感激。
答案 0 :(得分:1)
在您调用client.setCallback(new PushCallback(this));
的范围内,此是 AsyncTask ,因为 AsyncTask 类不会扩展 ContextWrapper 它与构造函数不匹配。
将行更改为
client.setCallback(new PushCallback(LWMQService.this));
获取正确的上下文对象