当前位置: 代码迷 >> Android >> 检察android客户端网络是否可用
  详细解决方案

检察android客户端网络是否可用

热度:92   发布时间:2016-05-01 16:24:15.0
检查android客户端网络是否可用

/**

*检查网络是否可用

*

*/

public class CheckNetWorkUtil? {

?
?public static boolean checkNetWork(Context context){
??//判断网络是否可用,如果不可用,给出提示
??????? boolean isAvailable = netWorkIsAvailable(context);
??????? if(!isAvailable){//如果不可用
?????? ? openDialog(context);
?????? ? return false;
??????? }
??????? return true;
?}
?
?public static boolean netWorkIsAvailable(Context context) {
??ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
??NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
??if(activeNetInfo!=null){
???if(activeNetInfo.isAvailable()){
????return true;
???}else{
????return false;
???}
??}
??return false;
?}
?
?private static void openDialog(final Context context) {
??final Builder builder=new AlertDialog.Builder(context);
??builder.setTitle("没有可用的网络");
??????? builder.setMessage("请开启GPRS或WIFI网络连接");
??????? builder.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");
?????????????????? context.startActivity(mIntent);

?}
??????? }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
??????????? public void onClick(DialogInterface dialog, int whichButton) {
??????????????? dialog.cancel();
??????????? }
??????? }).create().show();

}
}

  相关解决方案