2015-11-27 16 views
0

我有一個應用程序,它聽指示的IP和端口。我瞭解如何連接,如何接收數據。很難理解wifi連接如何工作

的事情是:

  • 我不知道如何來確認連接是否SUCESSFUL
  • 我不明白如何檢測連接丟失
  • 我不瞭解如何退出我的AsyncTask當我離開的應用程序(使用菜單)

這裏是用於獲取DATAS我的課:

public final class MyClientTask extends AsyncTask<Void, Void, Void> { 
    BufferedReader br = null; 
    String dstAddress; 
    int dstPort; 
    String response = ""; 

    public MyClientTask(String addr, int port){ 
     dstAddress = addr; 
     dstPort = port; 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     Socket socket = null; 
     try { 
      socket = new Socket(dstAddress, dstPort); 

      ByteArrayOutputStream byteArrayOutputStream = 
        new ByteArrayOutputStream(1024); 
      byte[] buffer = new byte[1024]; 

      //Instanciation de l'inputStreamReader 
      InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); 

      // creation nouveau bufferreader 
      br = new BufferedReader(inputStreamReader); 

      StringBuilder msgBuilder = new StringBuilder(); 
      // on évite String = String + char qui fait des créations de builders automatiques en java. 
      boolean ended = false; 
      while (!ended) { 
       int nextChar = inputStreamReader.read(); 
       if (nextChar == -1) { 
        ended = true; // <= fin du stream 
       } else if (nextChar == 03) { 

        String maChaine=""; 
        // fin du message 
        maChaine = msgBuilder.toString(); 

        //récupération du char après <STX> 
        Character nbChamps = maChaine.charAt(1); 
        //test pour savoir si c'est une alerte 
        if(nbChamps == '7'){ 
         //insertion de l'alerte dans la BDD 
         BDDAlerte.insertAlerte1(maChaine); 
         //creation de la notif 
         createNotification(); 

        } 
        //ici les tests splits pour le nombre de champs et inserer dans BdD ou faire ce qu'il faut 

        //suppVieillesAlertes(); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          //find listview to populate 
          final ListView lvItems = (ListView)findViewById(R.id.listView1); 
          //setup cursor adapter using Cursor from last step 
          final AlerteAdapter todoAdapter = new AlerteAdapter(getBaseContext(), BDDAlerte.getAllRows(),0); 

          lvItems.setAdapter(todoAdapter); 
         } 
        }); 
        // réinitialisation du builder 
        msgBuilder.setLength(0); 
       } else { 
        msgBuilder.append((char)nextChar); 
       } 
      } 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      response = "UnknownHostException: " + e.toString(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      response = "IOException: " + e.toString(); 
     } 
     finally{ 
      if(socket != null){ 
       try { 
        socket.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
     return null; 
    } 
} 

我退出按鈕位於我的應用程序的菜單中onOptionsItemSelected。

如果我沒有給出所有的數據,我在這裏要清理一些事情。

西蒙

回答

0
private boolean isNetworkAvailable() { 
    ConnectivityManager connectivityManager 
     = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
    return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

之前構建一個插座,如果讀取數據的IO異常被拋出,就意味着失去連接的網絡狀態,應首先檢查

您可以定義一個布爾場「運行」,最初的真實,將其設置爲false時,應用程序退出,檢查它的價值在你while循環是這樣的:

while (!ended&&running) { 
      int nextChar = inputStreamReader.read(); 
      if (nextChar == -1) { 
       ended = true; // <= fin du stream 
      } 
// ...... 
} 
0

首先製作一個單獨的類來檢查連接並通過調用不僅僅是進一步進行套接字的方法把條件..

公共類MobileConnectivity {

public static NetworkInfo getNetworkInfo(Context context) { 

     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     return cm.getActiveNetworkInfo(); 
    } 

    public static boolean isConnected(Context context) { 

     NetworkInfo info = MobileConnectivity.getNetworkInfo(context); 
     return (info != null && info.isConnected()); 
    } 

    public static boolean isConnectedWifi(Context context) { 

     NetworkInfo info = MobileConnectivity.getNetworkInfo(context); 
     return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI); 
    } 

    public static boolean isConnectedMobile(Context context) { 
     NetworkInfo info = MobileConnectivity.getNetworkInfo(context); 
     return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE); 
    } 

    public static boolean isConnectedFast(Context context) { 
     NetworkInfo info = MobileConnectivity.getNetworkInfo(context); 
     return (info != null && info.isConnected() 
       && MobileConnectivity.isConnectionFast(info.getType(), info.getSubtype())); 
    } 

    public static boolean isConnectionFast(int type, int subType) { 
     if (type == ConnectivityManager.TYPE_WIFI) { 
      return true; 
     } else if (type == ConnectivityManager.TYPE_MOBILE) { 
      switch (subType) { 
      case TelephonyManager.NETWORK_TYPE_1xRTT: 
       return false; // ~ 50-100 kbps 
      case TelephonyManager.NETWORK_TYPE_CDMA: 
       return false; // ~ 14-64 kbps 
      case TelephonyManager.NETWORK_TYPE_EDGE: 
       return false; // ~ 50-100 kbps 
      case TelephonyManager.NETWORK_TYPE_EVDO_0: 
       return true; // ~ 400-1000 kbps 
      case TelephonyManager.NETWORK_TYPE_EVDO_A: 
       return true; // ~ 600-1400 kbps 
      case TelephonyManager.NETWORK_TYPE_GPRS: 
       return false; // ~ 100 kbps 
      case TelephonyManager.NETWORK_TYPE_HSDPA: 
       return true; // ~ 2-14 Mbps 
      case TelephonyManager.NETWORK_TYPE_HSPA: 
       return true; // ~ 700-1700 kbps 
      case TelephonyManager.NETWORK_TYPE_HSUPA: 
       return true; // ~ 1-23 Mbps 
      case TelephonyManager.NETWORK_TYPE_UMTS: 
       return true; // ~ 400-7000 kbps 
      /* 
      * Above API level 7, make sure to set android:targetSdkVersion to 
      * appropriate level to use these 
      */ 
      case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 
       return true; // ~ 1-2 Mbps 
      case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 
       return true; // ~ 5 Mbps 
      case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 
       return true; // ~ 10-20 Mbps 
      case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 
       return false; // ~25 kbps 
      case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 
       return true; // ~ 10+ Mbps 
      // Unknown 
      case TelephonyManager.NETWORK_TYPE_UNKNOWN: 
      default: 
       return false; 
      } 
     } else { 
      return false; 
     } 
    }