android 登录前检查网络状态

来源:互联网 发布:爱live软件破解 编辑:程序博客网 时间:2024/04/29 05:51

http://1002878825-qq-com.iteye.com/blog/1194801


Java代码  收藏代码
  1. package com.dx;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.net.ConnectivityManager;  
  7. import android.net.NetworkInfo;  
  8. import android.net.NetworkInfo.State;  
  9. import android.os.Bundle;  
  10. import android.widget.TextView;  
  11.   
  12. public class Main extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17. //        setContentView(R.layout.main);  
  18.         TextView textView = new TextView(this);  
  19.         textView.setText("网络检测");  
  20.         setContentView(textView);  
  21.           
  22.         if(checkNetWorkInfo()){  
  23.             goToNetWork();  
  24.         };  
  25.     }  
  26.   
  27.     private boolean goToNetWork() {  
  28.         // TODO Auto-generated method stub  
  29.         ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  30.         NetworkInfo info = connectivityManager.getActiveNetworkInfo();  
  31.         if(info == null || !info.isAvailable()){  
  32.             new AlertDialog.Builder(this).setMessage("没有可以使用的网络").setPositiveButton("Ok"null).show();      
  33.             return false;  
  34.         }  
  35.         else{  
  36.             new AlertDialog.Builder(this).setMessage("网络正常可以使用").setPositiveButton("Ok"null).show();      
  37.             return true;  
  38.         }  
  39.           
  40.           
  41.     }  
  42.   
  43.     private boolean checkNetWorkInfo() {  
  44.         // TODO Auto-generated method stub  
  45.         ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  46.         State wifi  = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
  47.         if(wifi != null){  
  48.             new AlertDialog.Builder(this).setMessage(wifi.toString()).setPositiveButton("wifi"null).show();//显示wifi网络连接状态      
  49.             return true;  
  50.               
  51.         }else{  
  52.             State mobile  = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
  53.             if(mobile != null){  
  54.                 new AlertDialog.Builder(this).setMessage(mobile.toString()).setPositiveButton("3G"null).show();//显示3G网络连接状态      
  55.                 return true;  
  56.             }  
  57.         }  
  58.         return false;  
  59.           
  60.     }  
  61. }  



在手机应用与网络交互数据的时候,我们首先要判断有没有可用的网络,如果没有则跳到相应的网络设置页面,方法详见代码:

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("android.intent.action.VIEW");                   startActivity(mIntent);               }           }).setNeutralButton("取消", new DialogInterface.OnClickListener() {               public void onClick(DialogInterface dialog, int whichButton) {                   dialog.cancel();               }           }).create();           b.show();       }        return flag;   }


原创粉丝点击