0

我的 IntentService 类的代码是:

public class UdpSendService extends IntentService {
//...variable declarations
public UdpSendService() {
    // TODO Auto-generated constructor stub
    super("UdpService");
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    host = intent.getStringExtra("host");
    port = intent.getIntExtra("port", 4000);
    pType= intent.getIntExtra("pType", 0);
    delay = intent.getIntExtra("delay", 0);
    msg= intent.getStringExtra("msg");
    broadcastIntent = new Intent(UdpResponseReceiver.ACTION_RESP);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    try {
        addr = InetAddress.getByName(host);
        // addr=InetAddress.getLocalHost();
        socket = new DatagramSocket();
        // socket.connect(addr,port);

        System.out.println("\nSocket Connected");
    } catch (Exception e) {
        System.out.println("\nConnection failed");
        return;
    }

    send=true;
    switch (pType) {
    case 0:
        while (send) {
            sendPacket(msg);
        }
    case 1:
        // Float f = new Float(delayField.getString());
        // for(int i=0;i<2000;i++)
        while (send) {
            try {
                Thread.currentThread().sleep(delay);
            } catch (Exception e) {
            }
            sendPacket(msg);
        }
        // break;
    case 2:
        // for(int i=0;i<10000;i++)
        while (send) {
            int u = want(30);
            // int e=(int)Math.floor(-1*log(u)/(log((float)2.718)*2));
            // System.out.println(e);
            String data1 = "";
            while ((u--) > 0)
                data1 = data1 + msg;
            sendPacket(data1);
        }
        // break;
    case 3:
        //float f2 = Float.parseFloat(delayField.getText().toString());
        // for(int i=0;i<50;i++)
        while (send) {
            int u = want(30);
            // int e=(int)Math.floor(-1*log(u)/(log((float)2.718)*2));
            System.out.println(u);
            String data1 = "";
            while ((u--) > 0)
                data1 = data1 + msg;
            System.out.println("data length :" + data1.length());
            try {
                Thread.currentThread().sleep(delay);
            } catch (Exception e) {
            }
            sendPacket(data1);
        }

    }
}
public void onDestroy(){

    super.onDestroy();
    send=false;
    socket.close();
    socket=null;

}
void sendPacket(String text) {

    try {
        System.out.println("\nClient:: Sending packet: " + " to " + addr
                + port);
        byte[] data = text.getBytes();
        spacket = new DatagramPacket(data, data.length, addr, port);
        socket.send(spacket);
        String resultTxt="Sent Packet at:"+DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());
        broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);
        sendBroadcast(broadcastIntent);
    } catch (Exception e) {
        System.out.println("Error:" + e.getMessage());
        e.printStackTrace();
        // e.printStackTrace();
        return;
    }
}
}

我通过调用从活动中停止服务

stopService(udpServiceIntent); //I start the service using the same intent

logcat 显示:

01-14 15:53:41.446: W/System.err(1176): java.lang.NullPointerException
01-14 15:53:41.456: W/System.err(1176):     at com.example.udpmessageclient.UdpService.sendPacket(UdpService.java:123)
01-14 15:53:41.466: W/System.err(1176):     at com.example.udpmessageclient.UdpService.onHandleIntent(UdpService.java:74)
01-14 15:53:41.466: W/System.err(1176):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
01-14 15:53:41.466: W/System.err(1176):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 15:53:41.466: W/System.err(1176):     at android.os.Looper.loop(Looper.java:137)
01-14 15:53:41.476: W/System.err(1176):     at android.os.HandlerThread.run(HandlerThread.java:60)

据我了解,该程序仍会尝试执行

socket.send(spacket);

即使在服务停止并且套接字关闭之后。如何避免这种情况?停止服务和释放资源的正确方法是什么?

4

0 回答 0