0

我在我的服务中使用了一个 mqtt 服务器连接,它不断地向服务器发出频繁的请求。我的服务突然崩溃,使我的 ui 无响应,活动崩溃但应用程序没有崩溃,并且当服务崩溃时不会调用 ondestroy 方法。这是服务崩溃时的logcat:

10-21 10:34:12.841: W/ActivityThread(10204): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
10-21 10:34:13.561: A/libc(10204): Fatal signal 11 (SIGSEGV) at 0x003a003d (code=1)
10-21 10:34:18.061: E/PhonePolicy(10577): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback

这是我的服务代码:

package com.voltytrack;

public class SubscribeService extends Service implements MqttCallback {

public static final String BROKER_URL = "tcp://27.6.120.57:1883";

String clientId = null;
public  static MqttClient mqttClient = null;

final String ROUTEPOINTSTOPIC = "GPS/DATA/ROUTE";
final String BUSLOCTOPIC = "DEMO/CURRENTLOCATION";
final String GETLISTTOPIC = "GPS/DATA/HISTORY/LIST/"+LoginActivity.deviceid;
final String HISTORYTOPIC = "GPS/DATA/HISTORY/"+LoginActivity.deviceid;
String USERNAME = null, PASSWORD = null;
Intent connectedintent;
static Timer conntimer;
public static final String DEBUG_TAG = "MqttService"; // Debug TAG
private static final String MQTT_THREAD_NAME = "MqttService[" + DEBUG_TAG + "]"; // Handler Thread ID
private Handler mConnHandler;     // Seperate Handler thread for networking
HandlerThread hthread = new HandlerThread(MQTT_THREAD_NAME);

@Override
public void onCreate() {
    super.onCreate();
    registerReceiver(businforeceiver, new IntentFilter("SUBSCRIBERECEIVER"));
    hthread.start();
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    long memory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
    Log.i("Memory", String.valueOf(memory));
        USERNAME = intent.getStringExtra("username");
        PASSWORD = intent.getStringExtra("password");
    try {

        mConnHandler = new Handler(hthread.getLooper());
        mConnHandler.post(new Runnable() {
            @Override
            public void run() {

            try {
                connectedintent = new Intent("CONNECTED");
                MqttConnectOptions connoptions = new MqttConnectOptions();
                connoptions.setCleanSession(false);
                connoptions.setUserName(USERNAME);
                connoptions.setPassword(PASSWORD.toCharArray());
                connoptions.setConnectionTimeout(10);
                clientId = "and" + Math.random();
                mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
                mqttClient.setCallback(SubscribeService.this);
                mqttClient.connect(connoptions);
                if(mqttClient.isConnected()){
                    connectedintent.putExtra("CONN_STATUS", "Connection Successful");
                }
                sendBroadcast(connectedintent);

                Toast.makeText(getApplicationContext(), "Contacting Server...", Toast.LENGTH_SHORT).show();

            }catch(MqttSecurityException e) {
                connectedintent.putExtra("CONN_STATUS", "Invalid credentials");
                sendBroadcast(connectedintent);
                e.printStackTrace();
            } 
            catch(MqttException e) {
                e.printStackTrace();
                connectedintent.putExtra("CONN_STATUS", "Connection failed");
                sendBroadcast(connectedintent);
//                  Toast.makeText(getApplicationContext(), "Cannot Connect to Server!", Toast.LENGTH_SHORT).show();
                Log.i("not connected", e.getMessage());
            }catch (IllegalArgumentException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Server Error, Try Again!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } 

return Service.START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
    unregisterReceiver(businforeceiver);
    try {
//          mqttClient.unsubscribe(TOPIC);
        if(mqttClient.isConnected()){
            mqttClient.disconnect();
        }
    } catch (MqttException e) {
        Toast.makeText(getApplicationContext(), "Something went wrong!" + e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
 super.onDestroy();
}
public BroadcastReceiver businforeceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String PUBLISHTOPIC = intent.getExtras().getString("PUBLISHTOPIC");
        String SUBSCRIBETOPIC = intent.getExtras().getString("SUBSCRIBETOPIC");
        String DATA = intent.getExtras().getString("TRAVERSE");

        try {

            if(null!=SUBSCRIBETOPIC){
                String[] topic = SUBSCRIBETOPIC.split("@");
                mqttClient.subscribe(topic[0]);
                mqttClient.subscribe(topic[1]);
            }
            if(null != PUBLISHTOPIC){
                MqttMessage msg1 = new MqttMessage();
                msg1.setPayload(DATA.getBytes());
                msg1.setQos(0);
                mqttClient.getTopic(PUBLISHTOPIC).publish(msg1);
            }
        } catch (MqttPersistenceException e) {
            Log.e("BROADCAST SERVICE", e.getMessage());
        } catch (MqttException e) {
            Log.e("BROADCAST SERVICE", e.getMessage());
        }
    }
};
public void connectionLost(Throwable arg0) {
    try {

        if(null!=conntimer){
            conntimer.cancel();
        }

        MqttConnectOptions connoptions1 = new MqttConnectOptions();
        connoptions1.setCleanSession(false);
        connoptions1.setUserName(USERNAME);                            
        connoptions1.setPassword(PASSWORD.toCharArray());
        mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
        mqttClient.setCallback(SubscribeService.this);
        mqttClient.connect(connoptions1);
        if(mqttClient.isConnected()){

            mqttClient.subscribe(BUSLOCTOPIC);
            mqttClient.subscribe(HISTORYTOPIC);
            Log.i("conn_status", "connected");
        }
        else{
            Log.i("conn_status", "not connected");
        }

    } catch (MqttSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MqttException e) {
        // TODO Auto-generated catch block
        conntimer = new Timer();
        conntimer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                connectionLost(null);
            }
        }, 0, 1000);

        e.printStackTrace();
        Log.i("not connected", e.getMessage());
    } 
}
@Override
public void deliveryComplete(MqttDeliveryToken arg0) {
    // TODO Auto-generated method stub

}
@Override
public void messageArrived(MqttTopic arg0, MqttMessage arg1)
        throws Exception {
    if(GETLISTTOPIC.matches(arg0.toString())){
        Intent busdataintent = new Intent("BUSLIST");
        busdataintent.putExtra("buslist", arg1.toString());
        sendBroadcast(busdataintent);
    }
    else if(HISTORYTOPIC.matches(arg0.toString())){
        Intent buspointsintent = new Intent("BUSPOINTS");
        buspointsintent.putExtra("buspoints", arg1.toString());
        sendBroadcast(buspointsintent);
    }
    else if(ROUTEPOINTSTOPIC.matches(arg0.toString())){
        Intent userbusintent = new Intent("USERBUSDATA");
        userbusintent.putExtra("boardingpoints", arg1.toString());
        sendBroadcast(userbusintent);

    }

}

}

可能是什么原因?

4

0 回答 0