import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.View.OnClickListener;
import com.chansh.csm.R;
import com.chansh.csm.util.ExitApplication;
import com.umeng.analytics.MobclickAgent;
/**
*
* @ClassName: UIConnect
* @Description: TODO(判断网络、弹窗)
*
*
*/
public class UIConnect {
public static CustomAlertDialog c = null;
public static boolean IsConnect(Context context) {
if (!isConnectInternet(context)) {
return false;
} else {
return true;
}
}
public static boolean isConnectInternet(Context context) {
ConnectivityManager conManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null) { // 注意,这个判断一定要的哦,要不然会出错
return networkInfo.isAvailable();
}
return false;
}
/**
* 弹出无网络 取消不销毁当前页
*
*/
public static void configWifi(final Context context) {
c = new CustomAlertDialog(context);
c.setTitle(context.getString(R.string.app_name));
c.setMessage("没有可用的网络,是否设置网络?");
c.setCancelable(false);
c.setOnPositiveButton("设置", new OnClickListener() {
public void onClick(View v) {
c.dismiss();
Intent intent = null;
// 判断手机系统的版本 即API大于10 就是3.0或以上版本
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
((Activity) context).startActivityForResult(intent, 0);
}
});
c.setOnNeutralButton("取消", new OnClickListener() {
public void onClick(View v) {
c.dismiss();
}
});
c.show();
}
/**
* 提示用户退出系统
*
* @param context
*/
public static void show_eixt(final Context context) {
c = new CustomAlertDialog(context);
c.setTitle(context.getString(R.string.app_name));
c.setMessage("是否退出应用?");
c.setCancelable(true);
c.setOnPositiveButton("退出", new OnClickListener() {
public void onClick(View v) {
c.dismiss();
MobclickAgent.onKillProcess(context);
ExitApplication.getInstance().exit();
// 返回键最小化程序
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.addCategory(Intent.CATEGORY_HOME);
// context.startActivity(intent);
}
});
c.setOnNeutralButton("取消", new OnClickListener() {
public void onClick(View v) {
c.dismiss();
}
});
try {
c.show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param context
* @return 0:无网络 1:wifi网络 2:手机网络
*/
public static int isConnect_Type(Context context) {
ConnectivityManager conManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { // 判断是否是wifi网络
return 1;
} else if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { // 判断是否是手机网络
return 2;
}
return 0;
}
/**
* 判断网络连接
*
* @Title: isConn
* @return void 返回类型
* @throws
*/
public static void isConn(Context context) {
if (!isConnectInternet(context)) {
configWifi(context);
}
}
}
Android判断网络、弹窗
最新推荐文章于 2024-02-01 15:48:08 发布