package com.hdl.photovoltaic.utils;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiManager;
|
|
import com.hdl.photovoltaic.HDLApp;
|
|
import java.net.Inet4Address;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.net.SocketException;
|
import java.util.Enumeration;
|
import java.util.Iterator;
|
import java.util.Stack;
|
|
/**
|
* Author: Zoro
|
* Date: 2019/6/9
|
* Description: This is AppManagerUtils
|
*/
|
|
public class AppManagerUtils {
|
|
private static AppManagerUtils appManagerUtils;
|
|
private AppManagerUtils() {
|
}
|
|
public static AppManagerUtils getAppManager() {
|
if (null == appManagerUtils) {
|
synchronized (AppManagerUtils.class) {
|
if (null == appManagerUtils) {
|
appManagerUtils = new AppManagerUtils();
|
}
|
}
|
}
|
return appManagerUtils;
|
}
|
|
/**
|
* 获取AppVersion
|
*
|
* @param context -
|
* @return -
|
*/
|
public static String getAppVersion(Context context) {
|
String version = "";
|
try {
|
String packageName = context.getPackageName();
|
PackageManager packageManager = context.getPackageManager();
|
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
|
version = packageInfo.versionName;
|
} catch (PackageManager.NameNotFoundException e) {
|
e.printStackTrace();
|
}
|
return version;
|
}
|
|
/**
|
* 默认的Activity堆栈
|
*/
|
private Stack<Activity> activityStack;
|
|
/**
|
* 添加Activity到堆栈
|
*/
|
public void addActivity(Activity activity) {
|
if (activityStack == null) {
|
activityStack = new Stack<>();
|
}
|
activityStack.add(activity);
|
}
|
|
|
/**
|
* 移除Activity
|
*/
|
public void removeActivity(Activity activity) {
|
if (activity != null) {
|
activityStack.remove(activity);
|
}
|
}
|
|
/**
|
* 结束指定类名的Activity
|
*/
|
public void finishActivity(Class<?>... args) {
|
Iterator<Activity> iterator = activityStack.listIterator();
|
while (iterator.hasNext()) {
|
Activity activity = iterator.next();
|
for (Class<?> cls : args) {
|
if (activity.getClass().equals(cls)) {
|
activity.finish();
|
iterator.remove();
|
}
|
}
|
}
|
}
|
|
/**
|
* 指定一个类名,从指定类名开始移除后面所有Activity
|
*
|
* @param className Activity-类名(activity.getClass().getName())
|
* @param removeClass 是否移除本身(true-移除)
|
*/
|
public void finishActivity(String className, boolean removeClass) {
|
Iterator<Activity> iterator = activityStack.listIterator();
|
while (iterator.hasNext()) {
|
Activity activity = iterator.next();
|
if (activity.getClass().getName().equals(className)) {
|
if (removeClass) {
|
activity.finish();
|
iterator.remove();
|
}
|
break;
|
} else {
|
activity.finish();
|
iterator.remove();
|
}
|
}
|
|
// int count= activityStack.size() - 1;
|
// for (Iterator<Activity> it = activityStack.listIterator(count); it.hasNext(); ) {
|
// Activity activity = it.next();
|
// System.out.println("移除结束" + activity.getClass().getName());
|
// if (activity.getClass().getName().equals(className)) {
|
// if (removeClass) {
|
// activity.finish();
|
// it.remove();
|
// }
|
// break;
|
// } else {
|
// activity.finish();
|
// it.remove();
|
// }
|
// }
|
}
|
|
/**
|
* 结束所有Activity
|
*/
|
public void finishAllActivity() {
|
for (int i = 0, size = activityStack.size(); i < size; i++) {
|
if (null != activityStack.get(i)) {
|
Activity activity = activityStack.get(i);
|
if (!activity.isFinishing()) {
|
activity.finish();
|
}
|
}
|
}
|
activityStack.clear();
|
}
|
|
|
/**
|
* 获取ActivitySize
|
*/
|
public int getActivitySize() {
|
return activityStack.size();
|
}
|
|
// /**
|
// * 判断当前应用是否是debug状态
|
// */
|
// public static boolean isApkInDebug(Context context) {
|
// try {
|
// ApplicationInfo info = context.getApplicationInfo();
|
// return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
|
// } catch (Exception e) {
|
// 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);
|
}
|
|
|
}
|