我一直在遵循以下示例来创建Android服务(具体来说是MQTT服务连接):Implement Eclipse MQTT Android Client using single connection instance,并且在前景/背景中运行时效果很好,但是在终止应用程序时魔术就结束了。据我了解,它应该重新运行该过程,但事实并非如此,它也不再能够接收消息。
我的MqttConnectionManagerService.class
如下:
public class MqttConnectionManagerService extends Service {
private static final String TAG = "MqttConnectionManagerSe";
private MqttAndroidClient client;
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
connect();
return START_STICKY;
}
public void connect() {
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(getApplication().getApplicationContext(), "tcp://192.168.100.5:1883",
clientId);
try {
IMqttToken token = client.connect();
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) {
Log.d(TAG, "messageArrived: Topic: " + topic + " Message: " + message);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "onSuccess: Successfully connected to broker!!!");
try {
client.subscribe("test", 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "onSuccess: Successfully subscribed to topic!!!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "onFailure");
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
}
我的MqttServiceStartReceiver.class
:
public class MqttServiceStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MqttConnectionManagerService.class));
}
}
我的Manifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gshp.mqttpersistencev2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:name=".MqttApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MqttServiceStartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="org.eclipse.paho.android.service.MqttService" />
<service android:name=".MqttConnectionManagerService" />
</application>
</manifest>
我的MainActivity.class
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, MqttConnectionManagerService.class));
}
}
到目前为止,我已经尝试过:创建一个从Application扩展的类,并声明一个等于它的变量,并在实例化MQTT Client(MqttConnectionManagerService.class
函数connect()
)时将其作为参数传递。 / p>
我真的希望有人能帮助我。
提前谢谢!