判断项目的网络是否可用然后打开设置面板

来源:互联网 发布:java获取一个月的天数 编辑:程序博客网 时间:2024/06/05 07:22
步骤一.
  1. package com.example.network;  
  2.   
  3. import android.content.Context;  
  4. import android.net.ConnectivityManager;  
  5. import android.net.NetworkInfo;  
  6.   
  7. public class NetworkUtil {  
  8.   
  9.     /** 
  10.      * 检查网络是否可用 
  11.      *  
  12.      * @param context 
  13.      * @return 
  14.      */  
  15.     public static boolean isNetworkAvailable(Context context) {  
  16.   
  17.         ConnectivityManager manager = (ConnectivityManager) context  
  18.                 .getApplicationContext().getSystemService(  
  19.                         Context.CONNECTIVITY_SERVICE);  
  20.   
  21.         if (manager == null) {  
  22.             return false;  
  23.         }  
  24.   
  25.         NetworkInfo networkinfo = manager.getActiveNetworkInfo();  
  26.   
  27.         if (networkinfo == null || !networkinfo.isAvailable()) {  
  28.             return false;  
  29.         }  
  30.   
  31.         return true;  
  32.     }  
  33.       

  1. }  




步骤二.
  1. package com.example.network;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.content.ComponentName;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.content.Intent;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.     }  
  22.   
  23.     @Override  
  24.     protected void onStart() {  
  25.   
  26.         Log.i("MainActivity""onStart");  
  27.   
  28.         if (!NetworkUtil.isNetworkAvailable(this)) {  
  29.             showSetNetworkUI(this);  
  30.         } else {  
  31.             Toast.makeText(this"网络可用..."0).show();  
  32.         }  
  33.   
  34.         super.onStart();  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onResume() {  
  39.   
  40.         Log.i("MainActivity""onStart");  
  41.   
  42.         super.onResume();  
  43.     }  
  44.   
  45.     /* 
  46.      * 打开设置网络界面 
  47.      */  
  48.     public void showSetNetworkUI(final Context context) {  
  49.         // 提示对话框  
  50.         AlertDialog.Builder builder = new Builder(context);  
  51.         builder.setTitle("网络设置提示")  
  52.                 .setMessage("网络连接不可用,是否进行设置?")  
  53.                 .setPositiveButton("设置"new DialogInterface.OnClickListener() {  
  54.   
  55.                     @Override  
  56.                     public void onClick(DialogInterface dialog, int which) {  
  57.                         // TODO Auto-generated method stub  
  58.                         Intent intent = null;  
  59.                         // 判断手机系统的版本 即API大于10 就是3.0或以上版本  
  60.                         if (android.os.Build.VERSION.SDK_INT > 10) {  
  61.                             intent = new Intent(  
  62.                                     android.provider.Settings.ACTION_WIFI_SETTINGS);  
  63.                         } else {  
  64.                             intent = new Intent();  
  65.                             ComponentName component = new ComponentName(  
  66.                                     "com.android.settings",  
  67.                                     "com.android.settings.WirelessSettings");  
  68.                             intent.setComponent(component);  
  69.                             intent.setAction("android.intent.action.VIEW");  
  70.                         }  
  71.                         context.startActivity(intent);  
  72.                     }  
  73.                 })  
  74.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  75.   
  76.                     @Override  
  77.                     public void onClick(DialogInterface dialog, int which) {  
  78.   
  79.                         dialog.dismiss();  
  80.                     }  
  81.                 }).show();  
  82.     }  
  83.   
  84.     @Override  
  85.     public boolean onCreateOptionsMenu(Menu menu) {  
  86.         // Inflate the menu; this adds items to the action bar if it is present.  
  87.         getMenuInflater().inflate(R.menu.main, menu);  
  88.         return true;  
  89.     }  
  90.   
  91. }  
步骤三.
在获得到json解析完之后在发message呈现adapter之前的时候   判断得到的entity是否为空  不为空的话往下走   
new Thread(){public void run() {NBANewsBiz biz=new NBANewsBiz();String json=biz.getNBAresult();NBAnewsJson parser=new NBAnewsJson();NBAnews entity = parser.parser(json);if (entity!=null) {Message msg=Message.obtain();msg.obj=entity;handler.sendMessage(msg);}};}.start();






记得在 Manifest文件中加入以下权限

[html] view plain copy
  1. <uses-permission android:name="android.permission.INTERNET"/>  
  2.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  

步骤4  对于选择的网络进行的一个判断是wifi还是mobile


private int NetworkState() {ConnectivityManager conn=(ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);NetworkInfo wifi = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);NetworkInfo mobile = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if (wifi.isAvailable()) {Toast.makeText(this, "wifi链接成功", Toast.LENGTH_SHORT).show();return 1;}else if(mobile.isAvailable()){Toast.makeText(this, "您连接的是mobile网络", Toast.LENGTH_SHORT).show();return 2;}else{return 0;}}


0 0
原创粉丝点击