/** 判断是否有网络 */
private boolean checkNetwork() {
boolean flag = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null)
flag = cwjManager.getActiveNetworkInfo().isAvailable();
if (!flag) {
Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("<span class=\"hilite\">android</span>.intent.action.VIEW");
startActivity(mIntent);
}
}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();
b.show();
}
return flag;
}
Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
http://www.cnblogs.com/mainroadlee/archive/2011/01/11/Android_Network_State_Checking_And_Setting.html
/**
* 检测是否开启wify或gprs
* @param context
* @return
*/
public boolean checkNetwork(final Context context){
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile 3G Data Network
State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//wifi
State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
if(mobile==State.CONNECTED||mobile==State.CONNECTING)
return true;
if(wifi==State.CONNECTED||wifi==State.CONNECTING)
return true;
Builder b = new android.app.AlertDialog.Builder(context).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
b.setPositiveButton("设置网络", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
// context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面
}
}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();
b.show();
return false;
}
App自动更新之通知栏下载
http://www.cnblogs.com/qianxudetianxia/archive/2011/04/12/2010919.html
分享到:
相关推荐
2、判断网络连接状态的方法(如mobile网络和wifi网络) 3、用广播WifiManager.NETWORK_STATE_CHANGED_ACTION和ConnectivityManager.CONNECTIVITY_ACTION都行,但判断连接是否成功时,用NetworkInfo的getState()方法...
判断网络是否可用: 注意!是判断网络是否可用,但网络可用不代表一定能上外网的! public static boolean isNetworkAvailable(Context context) { ConnectivityManager manager = (ConnectivityManager) context...
主要为大家详细介绍了Android通过ConnectivityManager检查网络状态的相关资料,感兴趣的小伙伴们可以参考一下
判断Android客户端网络连接状态ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean ...
本文实例讲述了Android编程判断网络连接是否可用的方法。分享给大家供大家参考,具体如下: 为了提高用户体验,我们在开发 android 应用的过程需要联网获取数据的时候我们首先要做的一步就是: 1.判断当前手机是否...
代码如下: ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_...
下面的代码用ConnectivityManager查询是活动网络连接判断是否有Internet连接。 代码如下: ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); ...
判断当前网络是否是wifi /** * 判断当前是否是wifi * @param mContext * @return */ private static boolean isWifi(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) ...
App判断用户是否联网是很普遍的需求,实现思路大概有下面几种 ...1、判断网络是否已经连接 // check all network connect, WIFI or mobile public static boolean isNetworkAvailable(final Context context) {
Android里判断是否可以上网,常用的是如下方法: /** * 检测网络是否连接 * * @return */ private boolean ... // 去进行判断网络是否连接 if (manager.getActiveNetworkInfo() != null) { return manager
# 判断联网类型(1).jsif (connectivityManager == null) {let type = info.getType();
本文实例讲述了Android判断网络类型的方法。分享给大家供大家参考,具体如下: 判断网络类型是wifi,还是3G,还是2G网络,对不同的网络进行不同的处理,现将判断方法整理给大家,以供参考 说明:下面用到的数据移动...
网络开发是Android程序设计一个非常重要的内容,今天本文就和大家一起分享一下Android网络开发的...很多时候我们需要判断用户是否开启网络设置,通常通过ConnectivityManager类来判断网络连接是否存在。 获取网络状态:
//用于判断是否有网络 connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);//获取当前网络的连接服务 NetworkInfo info = connectivityManager....
Android中判断当前网络是否可用 应用场景:实现判断当前网络是否可用 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步骤: 1、获取ConnectivityManager对象 Context context = activity....
ConnectivityManager manager = (ConnectivityManager) ...//此处判断网络是否连接(注:此处只能判断网络是否连接,不能判断网络是否可用) if (networkInfo != null) return networkInfo.getState() == Netw
Android中判断网络是否连接实例详解 在android中,如何监测网络的状态呢,这个有的时候也是十分重要的,方法如下: public class ConnectionDetector { private Context _context; public ConnectionDetector...
”android.permission.ACCESS_NETWORK_STATE”> 1)判断是否有网络连接 代码如下:public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = ...
本文为大家分享了Android网络连接判断与相关处理,供...1)判断是否有网络连接 public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (Co
本文实例讲述了Android编程判断网络是否可用及调用系统设置项的方法。分享给大家供大家参考,具体如下: private boolean checkNetwork() { boolean flag = false; ConnectivityManager manager = ...