本篇文章為大家展示了如何在Android中對(duì)手機(jī)網(wǎng)絡(luò)進(jìn)行檢測(cè),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、網(wǎng)頁(yè)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、印江網(wǎng)站維護(hù)、網(wǎng)站推廣。
使用BroadcastReceiver廣播接收器來(lái)接收網(wǎng)絡(luò)狀態(tài)(采用系統(tǒng)廣播)
網(wǎng)絡(luò)權(quán)限
自定義廣播接收器
public class NetWorkStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("網(wǎng)絡(luò)狀態(tài)發(fā)生變化"); //檢測(cè)API是不是小于23,因?yàn)榈搅薃PI23之后getNetworkInfo(int networkType)方法被棄用 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { //獲得ConnectivityManager對(duì)象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //獲取ConnectivityManager對(duì)象對(duì)應(yīng)的NetworkInfo對(duì)象 //獲取WIFI連接的信息 NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); //獲取移動(dòng)數(shù)據(jù)連接的信息 NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已連接,移動(dòng)數(shù)據(jù)已連接", Toast.LENGTH_SHORT).show(); } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已連接,移動(dòng)數(shù)據(jù)已斷開", Toast.LENGTH_SHORT).show(); } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) { Toast.makeText(context, "WIFI已斷開,移動(dòng)數(shù)據(jù)已連接", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "WIFI已斷開,移動(dòng)數(shù)據(jù)已斷開", Toast.LENGTH_SHORT).show(); } //API大于23時(shí)使用下面的方式進(jìn)行網(wǎng)絡(luò)監(jiān)聽 }else { System.out.println("API level 大于23"); //獲得ConnectivityManager對(duì)象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //獲取所有網(wǎng)絡(luò)連接的信息 Network[] networks = connMgr.getAllNetworks(); //用于存放網(wǎng)絡(luò)連接信息 StringBuilder sb = new StringBuilder(); //通過(guò)循環(huán)將網(wǎng)絡(luò)信息逐個(gè)取出來(lái) for (int i=0; i < networks.length; i++){ //獲取ConnectivityManager對(duì)象對(duì)應(yīng)的NetworkInfo對(duì)象 NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]); sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected()); } Toast.makeText(context, sb.toString(),Toast.LENGTH_SHORT).show(); } } }
注冊(cè)廣播
動(dòng)態(tài)注冊(cè)
靜態(tài)注冊(cè)
動(dòng)態(tài)注冊(cè)
public class MainActivity extends AppCompatActivity { NetWorkStateReceiver netWorkStateReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //在onResume()方法注冊(cè) @Override protected void onResume() { if (netWorkStateReceiver == null) { netWorkStateReceiver = new NetWorkStateReceiver(); } IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(netWorkStateReceiver, filter); System.out.println("注冊(cè)"); super.onResume(); } //onPause()方法注銷 @Override protected void onPause() { unregisterReceiver(netWorkStateReceiver); System.out.println("注銷"); super.onPause(); } }
靜態(tài)注冊(cè)
在AndroidManifest.xml進(jìn)行廣播的靜態(tài)注冊(cè)
//檢測(cè)網(wǎng)絡(luò)變化的acton
上述內(nèi)容就是如何在Android中對(duì)手機(jī)網(wǎng)絡(luò)進(jìn)行檢測(cè),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。