Android判斷網路狀態方法詳解

來源:互聯網
上載者:User

標籤:

Android 判斷網路狀態這一應用技巧在實際應中是比較重要的。那麼,在Android作業系統中,如何能夠正確的判斷我們所串連的網路是否斷開恩?今天我們就針對這一應用技巧進行一個詳細的分析。

 

1.public class ConnectionChangeReceiver extends

BroadcastReceiver  

2.{  

[email protected]  

4.public void onReceive( Context context, Intent intent )  

5.{  

6.ConnectivityManager connectivityManager =

(ConnectivityManager) context.getSystemService

( Context.CONNECTIVITY_SERVICE );  

7.NetworkInfo activeNetInfo = connectivityManager.

getActiveNetworkInfo();  

8.NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo

( ConnectivityManager.TYPE_MOBILE );  

9.if ( activeNetInfo != null )  

10. {  

11.st.makeText( context, "Active Network Type : " +

  tiveNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();  

12. }  

13. mobNetInfo != null )  

14. {

15.st.makeText( context, "Mobile Network Type : " +

  bNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();  

16. }

17.}  

18.}  

19.< !-- Needed to check when the network connection changes -->  

20.< uses-permission android:name="android.permission.

ACCESS_NETWORK_STATE"/>  

21.< receiver android:name="com.blackboard.androidtest.

receiver.ConnectionChangeReceiver"  

22.android:label="NetworkConnection">  

23.< intent-filter>  

24.< action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>  

25.< /intent-filter>  

26.< /receiver>

 

Android判斷網路狀態的具體應用方法就為大家介紹到這裡。接下來給大家介紹一款我比較常用的APP測試載入器:www.ineice.com


Android判斷網路狀態方法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.