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

Android 檢測網絡是否連接

編輯:關於Android編程

權限:



代碼如下:

package com.example.nettest;

import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


private ConnectivityManager manager;
private TextView tv;
StringBuffer sb = new StringBuffer(256);


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


tv = (TextView) findViewById(R.id.textView1);
checkNetworkState();
}


/**
* 檢測網絡是否連接
*
* @return
*/
private boolean checkNetworkState() {
boolean flag = false;
// 得到網絡連接信息
manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// 去進行判斷網絡是否連接
if (manager.getActiveNetworkInfo() != null) {
flag = manager.getActiveNetworkInfo().isAvailable();
}
if (!flag) {
setNetwork();
} else {
isNetworkAvailable();
}
tv.setText(sb.toString());
return flag;
}


/**
* 網絡未連接時,調用設置方法
*/
private void setNetwork() {
Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("網絡提示信息");
builder.setMessage("網絡不可用,如果繼續,請先設置網絡!");
builder.setPositiveButton("設置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
/**
* 判斷手機系統的版本!如果API大於10 就是3.0+ 因為3.0以上的版本的設置和3.0以下的設置不一樣,調用的方法不同
*/
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
startActivity(intent);
}
});


builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create();
builder.show();
}


/**
* 網絡已經連接,然後去判斷是wifi連接還是GPRS連接 設置一些自己的邏輯調用
*/
private void isNetworkAvailable() {


State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
Toast.makeText(this, "gprs is open! ", Toast.LENGTH_SHORT).show();
sb.append("\ngprs is open! ");
} else {
sb.append("\ngprs is closed! ");
}


// 判斷為wifi狀態下才加載廣告,如果是GPRS手機網絡則不加載!
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
Toast.makeText(this, "wifi is open! ", Toast.LENGTH_SHORT).show();
loadAdmob();
sb.append("\nwifi is open! ");
} else {
sb.append("\nwifi is closed! ");
}


}


/**
* 在wifi狀態下 加載admob廣告
*/
private void loadAdmob() {
Toast.makeText(getApplicationContext(), "ad is loding..", 1).show();
sb.append("\nad is loding...");
}
}

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