今天也是没有什么好写的,但是自己的强迫症似乎又犯了,觉得每天写博客的习惯不能改,所以在网上搜索了资料,写了这些内容。
一。检查网络是否可用,并且弹出提示框,用户可以去设置网络。
package com.example.network;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtil {
/**
* 检查网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return false;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
return false;
}
return true;
}
}
这个主要通过手机的ConnectivityManager来获取当前的网络信息,然后判断是否有网络连接。
当网络不可用的时候,我们应该提供一个 界面让用户区设置网络连接。Android 2.3以上跟Android 2.3以前的设置方式是不一样的,我们需要判断一下。
package com.example.network;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
Log.i("MainActivity", "onStart");
if (!NetworkUtil.isNetworkAvailable(this)) {
showSetNetworkUI(this);
} else {
Toast.makeText(this, "网络可用...", 0).show();
}
super.onStart();
}
@Override
protected void onResume() {
Log.i("MainActivity", "onStart");
super.onResume();
}
/*
* 打开设置网络界面
*/
public void showSetNetworkUI(final Context context) {
// 提示对话框
AlertDialog.Builder builder = new Builder(context);
builder.setTitle("网络设置提示")
.setMessage("网络连接不可用,是否进行设置?")
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = null;
// 判断手机系统的版本 即API大于10 就是3.0或以上版本
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIFI_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");
}
context.startActivity(intent);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这里打当没有网络时,弹出设置网络对话框,用户点击确定时,可以直接跳到设置网络界面。
后面我还找了判断网络类型的代码,也很简单。
package
cn.hackcoder.beautyreader.utils;
import
android.content.Context;
import
android.net.ConnectivityManager;
import
android.net.NetworkInfo;
import
android.telephony.TelephonyManager;
/**
* Created by hackcoder on 15-1-25.
*/
public
class
NetWorkUtils {
/**
* 判断是否有网络连接
* @param context
* @return
*/
public
static
boolean
isNetworkConnected(Context context) {
if
(context !=
null
) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if
(mNetworkInfo !=
null
) {
return
mNetworkInfo.isAvailable();
}
}
return
false
;
}
/**
* 判断WIFI网络是否可用
* @param context
* @return
*/
public
static
boolean
isWifiConnected(Context context) {
if
(context !=
null
) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if
(mWiFiNetworkInfo !=
null
) {
return
mWiFiNetworkInfo.isAvailable();
}
}
return
false
;
}
/**
* 判断MOBILE网络是否可用
* @param context
* @return
*/
public
static
boolean
isMobileConnected(Context context) {
if
(context !=
null
) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if
(mMobileNetworkInfo !=
null
) {
return
mMobileNetworkInfo.isAvailable();
}
}
return
false
;
}
/**
* 获取当前网络连接的类型信息
* @param context
* @return
*/
public
static
int
getConnectedType(Context context) {
if
(context !=
null
) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if
(mNetworkInfo !=
null
&& mNetworkInfo.isAvailable()) {
return
mNetworkInfo.getType();
}
}
return
-
1
;
}
/**
* 获取当前的网络状态 :没有网络0:WIFI网络1:3G网络2:2G网络3
*
* @param context
* @return
*/
public
static
int
getAPNType(Context context) {
int
netType =
0
;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if
(networkInfo ==
null
) {
return
netType;
}
int
nType = networkInfo.getType();
if
(nType == ConnectivityManager.TYPE_WIFI) {
netType =
1
;
// wifi
}
else
if
(nType == ConnectivityManager.TYPE_MOBILE) {
int
nSubType = networkInfo.getSubtype();
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if
(nSubType == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
netType =
2
;
// 3G
}
else
{
netType =
3
;
// 2G
}
}
return
netType;
}
}
还有一个判断网络状态改变的代码,就是一个广播接收器。
package
cn.hackcoder.beautyreader.broadcast;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.net.ConnectivityManager;
import
android.util.Log;
import
android.widget.Toast;
import
cn.hackcoder.beautyreader.activity.base.BaseActivity;
import
cn.hackcoder.beautyreader.utils.NetWorkUtils;
public
class
NetWorkStatusReceiver
extends
BroadcastReceiver {
public
NetWorkStatusReceiver() {
}
@Override
public
void
onReceive(Context context, Intent intent) {
String action = intent.getAction();
if
(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Toast.makeText(context,
"network changed"
, Toast.LENGTH_LONG).show();
BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)>
0
;
}
}
}
需要在manifest里注册广播。
<receiver
android:name=
".broadcast.NetWorkStatusReceiver"
android:enabled=
"true"
android:exported=
"true"
>
<intent-filter>
<action android:name=
"android.net.conn.CONNECTIVITY_CHANGE"
/>
</intent-filter>
</receiver>
这些是我在网上搜索资料整理得来的,大家可以自己在网上搜素资料,其实很简单,网上资料也很多。
android 判断网络是否可用就讲完了。
就这么简单。