首页 > 代码库 > android 网络连接检测
android 网络连接检测
首先, 需要在AndroidManifest.xml 中添加下列权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
判断是否连接的方法类:
package com.akm.detectinternetconnectionstatus; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context) { this._context = context; } public boolean isConnectingToInternet() { ConnectivityManager connectivity = (ConnectivityManager) _context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } }
示例:
package com.akm.detectinternetconnectionstatus; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { ConnectionDetector cd; Boolean isInternetPresent = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnStatus = (Button) findViewById(R.id.btn_check); cd = new ConnectionDetector(getApplicationContext()); btnStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get Internet status isInternetPresent = cd.isConnectingToInternet(); // check for Internet status if (isInternetPresent) { // Internet Connection is Present // make HTTP requests showAlertDialog(MainActivity.this, "Internet Connection", "You have internet connection", true); } else { // Internet connection is not present // Ask user to connect to Internet showAlertDialog(MainActivity.this, "No Internet Connection", "You don’t have internet connection.", false); } } }); } /** * Function to display simple Alert Dialog * * @param context * – application context * @param title * – alert dialog title * @param message * – alert message * @param status * – success/failure (used to set icon) * */ public void showAlertDialog(Context context, String title, String message, Boolean status) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Setting Dialog Message alertDialog.setMessage(message); // Setting alert dialog icon alertDialog.setIcon(R.drawable.ic_launcher); // alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); // Setting OK Button alertDialog.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); // Showing Alert Message alertDialog.show(); } }
android 网络连接检测
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。