频道栏目
IT货架 > > 正文
android 网络连接检测
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:

首先, 需要在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 网络连接检测

标签:

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1