| | |
| | | import android.net.wifi.WifiManager; |
| | | |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.widget.LoadingDialog; |
| | | |
| | | import java.net.Inet4Address; |
| | | import java.net.InetAddress; |
| | |
| | | public class AppManagerUtils { |
| | | |
| | | private static AppManagerUtils appManagerUtils; |
| | | |
| | | private LoadingDialog loadingDialog; |
| | | |
| | | private AppManagerUtils() { |
| | | } |
| | |
| | | return activityStack.size(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前的Activity |
| | | * |
| | | * @return - |
| | | */ |
| | | public Activity getLastActivity() { |
| | | if (activityStack.size() > 0) { |
| | | return activityStack.get(activityStack.size() - 1); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取LoadingDialog |
| | | * |
| | | * @return LoadingDialog |
| | | */ |
| | | public LoadingDialog getLoadingDialog() { |
| | | Activity activity = getLastActivity(); |
| | | if (loadingDialog == null && activity != null) { |
| | | loadingDialog = new LoadingDialog(activity, R.style.Custom_Dialog); |
| | | } |
| | | return loadingDialog; |
| | | } |
| | | |
| | | /** |
| | | * 开始Loading |
| | | */ |
| | | public void showLoading() { |
| | | getLoadingDialog().start(); |
| | | } |
| | | |
| | | /** |
| | | * 开始Loading |
| | | * |
| | | * @param mes 自定义文本 |
| | | */ |
| | | public void showLoading(String mes) { |
| | | getLoadingDialog().start(); |
| | | getLoadingDialog().setText(mes); |
| | | } |
| | | |
| | | /** |
| | | * 停止隐藏Loading |
| | | */ |
| | | public void hideLoading() { |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (loadingDialog != null && loadingDialog.isShowing()) { |
| | | loadingDialog.stop(); |
| | | } |
| | | } |
| | | }, null, null); |
| | | } |
| | | |
| | | |
| | | // /** |
| | | // * 判断当前应用是否是debug状态 |
| | | // */ |
| | |
| | | // return false; |
| | | // } |
| | | // } |
| | | |
| | | /** |
| | | * 获得IP地址,分为两种情况: |
| | | * 一:是wifi下; |
| | | * 二:是移动网络下; |
| | | */ |
| | | public String getIPAddress(Context context) { |
| | | NetworkInfo info = ((ConnectivityManager) context |
| | | .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); |
| | | if (info != null && info.isConnected()) { |
| | | if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络 |
| | | try { |
| | | //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); |
| | | for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { |
| | | NetworkInterface intf = en.nextElement(); |
| | | for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { |
| | | InetAddress inetAddress = enumIpAddr.nextElement(); |
| | | if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { |
| | | return inetAddress.getHostAddress(); |
| | | } |
| | | } |
| | | } |
| | | } catch (SocketException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络 |
| | | WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
| | | WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
| | | //调用方法将int转换为地址字符串 |
| | | String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址 |
| | | return ipAddress; |
| | | } |
| | | } else { |
| | | //当前无网络连接,请在设置中打开网络 |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 将得到的int类型的IP转换为String类型 |
| | | * |
| | | * @param ip int类型 |
| | | * @return - |
| | | */ |
| | | public static String intIP2StringIP(int ip) { |
| | | return (ip & 0xFF) + "." + |
| | | ((ip >> 8) & 0xFF) + "." + |
| | | ((ip >> 16) & 0xFF) + "." + |
| | | (ip >> 24 & 0xFF); |
| | | } |
| | | |
| | | |
| | | } |