『壹』 如何判斷 android 網路狀態
在屏幕的上方有沒有E\G\H\3G的圖標,圖標下方是打對勾的還是上下箭頭?如果有就代表你的網路是開啟的
『貳』 android判斷移動網路是否打開
Android 判斷網路狀態這一應用技巧在實際應中是比較重要的。那麼,在Android操作系統中,如何能夠正確的判斷我們所連接的網路是否斷開:
public class ConnectionChangeReceiver extends
BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
}
< !-- Needed to check when the network connection changes -->
< uses-permission android:name="android.permission.
ACCESS_NETWORK_STATE"/>
< receiver android:name="com.blackboard.androidtest.
receiver.ConnectionChangeReceiver"
android:label="NetworkConnection">
< intent-filter>
< action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
< /intent-filter>
< /receiver>
『叄』 Android中如何簡單檢測網路是否連接
許可權:
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
代碼:
java">/*
*判斷網路連接是否已開
*true已打開false未打開
**/
publicstaticbooleanisConn(Contextcontext){
booleanbisConnFlag=false;
ConnectivityManagerconManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetwork=conManager.getActiveNetworkInfo();
if(network!=null){
bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
}
returnbisConnFlag;
}
/*沒有網路跳轉到網路設置頁面
*打開設置網路界面
**/
(finalContextcontext){
//提示對話框
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle("網路設置提示").setMessage("網路連接不可用,是否進行設置?").setPositiveButton("設置",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
//TODOAuto-generatedmethodstub
Intentintent=null;
//判斷手機系統的版本即API大於10就是3.0或以上版本
if(Build.VERSION.SDK_INT>10){
intent=newIntent(Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent=newIntent();
ComponentNamecomponent=newComponentName("com.android.settings","com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
}
}).setNegativeButton("取消",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
//TODOAuto-generatedmethodstub
dialog.dismiss();
}
}).show();
}
『肆』 android 如何判斷網路是否能夠上網
實現步驟:
下面解決辦法來自於android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載。排到第三個。
1、獲取ConnectivityManager對象
Context context = activity.getApplicationContext();
// 獲取手機所有連接管理對象(包括對wi-fi,net等連接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
2、獲取NetworkInfo對象
// 獲取NetworkInfo對象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
3、判斷當前網路狀態是否為連接狀態
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
4、在AndroidManifest.xml中添加訪問當前網路狀態許可權
<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(), "當前有可用網路!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "當前沒有可用網路!", Toast.LENGTH_LONG).show();
}
}
/**
* 檢查當前網路是否可用
*
* @param context
* @return
*/
public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 獲取手機所有連接管理對象(包括對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 + "===狀態===" + networkInfo[i].getState());
System.out.println(i + "===類型===" + networkInfo[i].getTypeName());
// 判斷當前網路狀態是否為連接狀態
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}