android 判斷是否有網(wǎng)絡(luò)
實現(xiàn)步驟:
1、獲取ConnectivityManager對象
Context context = activity.getApplicationContext();
// 獲取手機(jī)所有連接管理對象(包括對wi-fi,net等連接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
2、獲取NetworkInfo對象
// 獲取NetworkInfo對象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
3、判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)是否為連接狀態(tài)
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
4、在AndroidManifest.xml中添加訪問當(dāng)前網(wǎng)絡(luò)狀態(tài)權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
代碼如下:
public class ClassTestDemoActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (isNetworkAvailable(ClassTestDemoActivity.this))
{
Toast.makeText(getApplicationContext(), "當(dāng)前有可用網(wǎng)絡(luò)!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "當(dāng)前沒有可用網(wǎng)絡(luò)!", Toast.LENGTH_LONG).show();
}
}
/**
* 檢查當(dāng)前網(wǎng)絡(luò)是否可用
*
* @param context
* @return
*/
public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 獲取手機(jī)所有連接管理對象(包括對wi-fi,net等連接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null)
{
return false;
}
else
{
// 獲取NetworkInfo對象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0)
{
for (int i = 0; i < networkInfo.length; i++)
{
System.out.println(i + "===狀態(tài)===" + networkInfo[i].getState());
System.out.println(i + "===類型===" + networkInfo[i].getTypeName());
// 判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)是否為連接狀態(tài)
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
android判斷網(wǎng)絡(luò)是否連接
public class NetworkDetector {
public static boolean detect(Activity act) {
ConnectivityManager manager = (ConnectivityManager) act
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return false;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
return false;
}
return true;
}
} 這個類只有一個靜態(tài)方法,用來檢測當(dāng)前系統(tǒng)的網(wǎng)絡(luò)是否可用。如果可用,返回true
還有啥不知道可以繼續(xù)追問
android:name="android.permission.ACCESS_NETWORK_STATE" /> 最后不要忘了權(quán)限問題
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
// 有可用的網(wǎng)絡(luò)
}
0android判斷網(wǎng)絡(luò)是否連接
android項目判斷是否有可用網(wǎng)絡(luò)
從程序開發(fā)的角度來開發(fā)該功能如下代碼及代碼中的注釋即可理解:ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); // 檢查網(wǎng)絡(luò)連接,如果無網(wǎng)絡(luò)可用,就不需要進(jìn)行連網(wǎng)操作等 NetworkInfo info = manager.getActiveNetworkInfo(); if (info == null || !manager.getBackgroundDataSetting()) { return "無網(wǎng)絡(luò)鏈接"; } return "網(wǎng)絡(luò)暢通";
相關(guān)推薦:
民事訴訟偽造證據(jù)如何處理(民事訴訟中偽造證據(jù)的法律后果)
快遞損壞賠償包括哪些(快遞出現(xiàn)損壞如何賠償)
民事訴訟費用怎么算(訴訟費收費標(biāo)準(zhǔn)速算法)
醉駕標(biāo)準(zhǔn)及處罰規(guī)定(醉駕的標(biāo)準(zhǔn)是多少毫升)
中止訴訟時效的情形(刑法訴訟時效中斷的四種情形)