android 登录前检察网络状态
android 登录前检查网络状态
package com.dx;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager; import android.net.NetworkInfo;
import android.net.NetworkInfo.State; import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // setContentView(R.layout.main);
TextView textView = new TextView(this);
textView.setText( 网络检测 );
setContentView(textView);
if(checkNetWorkInfo()){
goToNetWork();
};
}
private boolean goToNetWork() {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if(info == null || !info.isAvailable()){
new AlertDialog.Builder(this).setMessage( 没有可以使用的网
络 ).setPositiveButton( Ok , null).show();
return false;
}
else{
new AlertDialog.Builder(this).setMessage( 网络正常可以使
用 ).setPositiveButton( Ok , null).show();
return true;
}
}
private boolean checkNetWorkInfo() {
// TODO Auto-generated method stub
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
State wifi =
manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(wifi != null){
new
AlertDialog.Builder(this).setMessage(wifi.toString()).setPositiveButton( wifi ,
null).show();//显示wifi网络连接状态
return true;
}else{
State mobile =
manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(mobile != null){
new
AlertDialog.Builder(this).setMessage(mobile.toString()).setPositiveButton( 3G ,
null).show();//显示3G网络连接状态
return true;
}
}
return false;
}
}
本文档为【android 登录前检察网络状态】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。