0

我有一個運行兩個asynctasks的服務。一個用於接收多播數據包,另一個用於接收udp數據包。我已經註冊了一個wifi狀態監聽器,並且當連接到一個接入點丟失時想要停止監聽數據包。但是,當我嘗試關閉其中一個套接字並嘗試結束while循環時,服務本身就會關閉。我希望服務能夠在整個過程中運行,直到我的活動結束。 以下是我的代碼的相關部分。支架可能會錯位...停止AsyncTask導致服務結束

public class ReceiveService extends Service { 

private Receive r; 
private ReceiveMulticast rm; 
private boolean running = true; 

private boolean receive = false, receivemulti = false; 


@Override 
public IBinder onBind(Intent arg0) { 
    // return binder; 
    return null; 
} 

@Override 
public void onCreate() { 

    try { 

     r = new Receive(); 
     rm = new ReceiveMulticast(); 


     if (wifi.checkwifi(this)) {//check if connected to access point 
      wifiInit(); 
      receive(); 
      receivemulti(); 
     } 

    } catch (Exception e) { 
     Log.d("test", "exception in oncreate"); 
     //e.printStackTrace(); 
    } 

} 

public void wifiInit() { 
    wm = wifi.getWifiManager(this); 
    myip = wifi.getmyip(wm); 
    wifi.acquirelock(wm); 
} 

public void receive() { 
    r.execute(); 
    receive = true; 

} 

public void receivemulti() { 
    rm.execute(); 
    receivemulti = true; 

} 

private BroadcastReceiver WifiStateChangedReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     //monitor changes in wifi state 
    } 
}; 

@Override 
public void onDestroy() { 
    try { 
     if (!r.ds1.isClosed()) 
      r.ds1.close(); 
     if (!rm.s.isClosed()) { 
      rm.s.leaveGroup(rm.group); 
      rm.s.close(); 
     } 

     super.onDestroy(); 
    } catch (Exception e) { 
     Log.d("test", "Exception in destroy"); 
    } 
} 

private class Receive extends AsyncTask<Void, String, Void> { 

    private DatagramSocket ds1; 
    private DatagramPacket p; 

    protected void onPreExecute() { 

    } 

    protected Void doInBackground(Void... params) { 

     try { 
      ds1 = new DatagramSocket(7777); 
     } catch (SocketException e) { 
      Log.d("test", "Exception in new datagram"); 
      //e.printStackTrace(); 
     } 
     int buffer_size = 1024; 
     byte buffer1[] = new byte[buffer_size]; 
     p = new DatagramPacket(buffer1, buffer1.length); 

     while (running) { 
      try { 
       ds1.receive(p); 
       String sip = p.getAddress().getHostAddress(); 
       String rec = new String(p.getData(), 0, p.getLength()); 

       //publishProgress(sip + "+" + rec); 
      } catch (Exception e) { 

       running = false; 
      } 
     } 
     return null; 

    } 

    protected void onProgressUpdate(String... progress) { 
     //do stuff 

    } 

    protected void onPostExecute(Void result) { 
     stopSelf(); 
    } 

} 

private class ReceiveMulticast extends AsyncTask<Void, String, Void> { 

    private String ip = "224.0.0.10"; 
    private int port = 6789; 
    private MulticastSocket s; 
    private DatagramPacket mp; 
    private InetAddress group; 

    protected void onPreExecute() { 

    } 

    protected Void doInBackground(Void... params) { 

     try { 
      group = InetAddress.getByName(ip); 
      s = new MulticastSocket(port); 
      s.joinGroup(group); 
     } catch (UnknownHostException e1) { 
      Log.d("test", "exception unknown host rm"); 

      //e1.printStackTrace(); 
     } catch (IOException e) { 
      Log.d("test", "exception io rm"); 

      //e.printStackTrace(); 
     } 

     byte[] buffer2 = new byte[1024]; 
     mp = new DatagramPacket(buffer2, buffer2.length); 

     while (running) { 

      try { 
       s.receive(mp); 
       String sip = mp.getAddress().getHostAddress(); 
       String rec = new String(mp.getData(), 0, mp.getLength()); 

       //publishProgress(sip + "+" + rec); 
      } catch (Exception e) { 

       running = false; 

      } 
     } 
     return null; 
    } 

    protected void onProgressUpdate(String... progress) { 

     //do stuff 


     } 

    } 

    protected void onPostExecute(Void result) { 
     stopSelf(); 
    } 
} 

} 

回答

0

正常停止或停止異常?

您在其中一個AsyncTasks的postExecute()中調用stopSelf()。如果你得到一個異常,你的while(true)...循環會退出,然後調用postExecute(),它調用stopSelf()。這可能是原因,但由於您沒有記錄異常,所以無法知道。

+0

感謝您指點我正確的方向。我從某處複製了asynctask模板,但沒有意識到stopself()用於停止服務而不是任務。完全忽略它。再次感謝。你爲我節省了很多時間...... – madridista 2012-03-19 18:05:15

-1

AsyncTask只能在UI線程上運行。