Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 網絡連接檢測

android 網絡連接檢測

編輯:關於Android編程

首先, 需要在AndroidManifest.xml 中添加下列權限:

  
  

判斷是否連接的方法類:

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();
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved