获取包名,补充监听内容占位
This commit is contained in:
@@ -4,11 +4,14 @@ import android.Manifest;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
@@ -19,6 +22,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
@@ -29,6 +33,7 @@ import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Enumeration;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -39,20 +44,20 @@ public class CommonUtils {
|
||||
private static String mCPUSerial = null;
|
||||
private static boolean isMacSerialNoObtained = false;
|
||||
private static boolean isCPUSerialNoObtained = false;
|
||||
private static final Pattern VERSION_NAME_PATTERN = Pattern.compile( "(\\d+\\.\\d+\\.\\d+)\\-*.*" );
|
||||
private static final Pattern VERSION_NAME_PATTERN = Pattern.compile("(\\d+\\.\\d+\\.\\d+)\\-*.*");
|
||||
|
||||
|
||||
public static String getAndroidID( Context context ) {
|
||||
if ( context == null ) {
|
||||
public static String getAndroidID(Context context) {
|
||||
if (context == null) {
|
||||
return "";
|
||||
}
|
||||
return Settings.Secure.getString( context.getContentResolver(), "android_id" );
|
||||
return Settings.Secure.getString(context.getContentResolver(), "android_id");
|
||||
}
|
||||
|
||||
public static String getCPUSerialno() {
|
||||
if ( !TextUtils.isEmpty( mCPUSerial ) ) {
|
||||
if (!TextUtils.isEmpty(mCPUSerial)) {
|
||||
return mCPUSerial;
|
||||
} else if ( isCPUSerialNoObtained ) {
|
||||
} else if (isCPUSerialNoObtained) {
|
||||
mCPUSerial = "";
|
||||
return mCPUSerial;
|
||||
} else {
|
||||
@@ -62,35 +67,35 @@ public class CommonUtils {
|
||||
LineNumberReader input = null;
|
||||
try {
|
||||
isCPUSerialNoObtained = true;
|
||||
Process ex = Runtime.getRuntime().exec( "cat /proc/cpuinfo" );
|
||||
if ( ex == null ) {
|
||||
Process ex = Runtime.getRuntime().exec("cat /proc/cpuinfo");
|
||||
if (ex == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ir = new InputStreamReader( ex.getInputStream() );
|
||||
input = new LineNumberReader( ir );
|
||||
ir = new InputStreamReader(ex.getInputStream());
|
||||
input = new LineNumberReader(ir);
|
||||
|
||||
while ( null != str ) {
|
||||
while (null != str) {
|
||||
str = input.readLine();
|
||||
if ( str != null ) {
|
||||
if (str != null) {
|
||||
mCPUSerial = str.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch ( IOException var4 ) {
|
||||
} catch (IOException var4) {
|
||||
var4.printStackTrace();
|
||||
} finally {
|
||||
if ( ir != null ) {
|
||||
if (ir != null) {
|
||||
try {
|
||||
ir.close();
|
||||
} catch ( IOException e ) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if ( input != null ) {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch ( IOException e ) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -100,20 +105,20 @@ public class CommonUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static int getVersionCode( Context context ) {
|
||||
public static int getVersionCode(Context context) {
|
||||
|
||||
if ( context == null ) {
|
||||
if (context == null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
String pkgName = context.getPackageName();
|
||||
|
||||
try {
|
||||
PackageInfo e = context.getPackageManager().getPackageInfo( pkgName, 0 );
|
||||
if ( e != null ) {
|
||||
PackageInfo e = context.getPackageManager().getPackageInfo(pkgName, 0);
|
||||
if (e != null) {
|
||||
return e.versionCode;
|
||||
}
|
||||
} catch ( Exception var2 ) {
|
||||
} catch (Exception var2) {
|
||||
var2.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -122,9 +127,9 @@ public class CommonUtils {
|
||||
|
||||
|
||||
public static String getMacSerialno() {
|
||||
if ( !TextUtils.isEmpty( mMacSerial ) ) {
|
||||
if (!TextUtils.isEmpty(mMacSerial)) {
|
||||
return mMacSerial;
|
||||
} else if ( isMacSerialNoObtained ) {
|
||||
} else if (isMacSerialNoObtained) {
|
||||
mMacSerial = "";
|
||||
return mMacSerial;
|
||||
} else {
|
||||
@@ -134,35 +139,35 @@ public class CommonUtils {
|
||||
LineNumberReader input = null;
|
||||
try {
|
||||
isMacSerialNoObtained = true;
|
||||
Process ex = Runtime.getRuntime().exec( "cat /sys/class/net/wlan0/address" );
|
||||
if ( ex == null ) {
|
||||
Process ex = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
|
||||
if (ex == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ir = new InputStreamReader( ex.getInputStream() );
|
||||
input = new LineNumberReader( ir );
|
||||
ir = new InputStreamReader(ex.getInputStream());
|
||||
input = new LineNumberReader(ir);
|
||||
|
||||
while ( null != str ) {
|
||||
while (null != str) {
|
||||
str = input.readLine();
|
||||
if ( str != null ) {
|
||||
if (str != null) {
|
||||
mMacSerial = str.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch ( IOException var4 ) {
|
||||
} catch (IOException var4) {
|
||||
var4.printStackTrace();
|
||||
} finally {
|
||||
if ( ir != null ) {
|
||||
if (ir != null) {
|
||||
try {
|
||||
ir.close();
|
||||
} catch ( IOException e ) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if ( input != null ) {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch ( IOException e ) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -177,27 +182,27 @@ public class CommonUtils {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getNetworkType( Context context ) {
|
||||
public static String getNetworkType(Context context) {
|
||||
|
||||
String name = "UNKNOWN";
|
||||
|
||||
try {
|
||||
|
||||
ConnectivityManager connMgr = ( ConnectivityManager ) context.getSystemService( Context.CONNECTIVITY_SERVICE );
|
||||
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||
if ( networkInfo != null ) {
|
||||
if ( ConnectivityManager.TYPE_WIFI == networkInfo.getType() ) {
|
||||
if (networkInfo != null) {
|
||||
if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
|
||||
return "WIFI";
|
||||
}
|
||||
}
|
||||
|
||||
TelephonyManager tm = ( TelephonyManager ) context.getSystemService( Context.TELEPHONY_SERVICE );
|
||||
if ( tm == null ) {
|
||||
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (tm == null) {
|
||||
return name;
|
||||
}
|
||||
|
||||
int type = tm.getNetworkType();
|
||||
switch ( type ) {
|
||||
switch (type) {
|
||||
case TelephonyManager.NETWORK_TYPE_GPRS:
|
||||
case TelephonyManager.NETWORK_TYPE_EDGE:
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||
@@ -226,7 +231,7 @@ public class CommonUtils {
|
||||
name = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return name;
|
||||
@@ -237,14 +242,14 @@ public class CommonUtils {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getIMEI( Context context ) {
|
||||
public static String getIMEI(Context context) {
|
||||
try {
|
||||
TelephonyManager telephonyManager = ( TelephonyManager ) context.getSystemService( Context.TELEPHONY_SERVICE );
|
||||
if ( telephonyManager != null &&
|
||||
ContextCompat.checkSelfPermission( context, Manifest.permission.READ_PHONE_STATE ) == PackageManager.PERMISSION_GRANTED ) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager != null &&
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
|
||||
return telephonyManager.getDeviceId();
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return "";
|
||||
@@ -255,35 +260,35 @@ public class CommonUtils {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getIMSI( Context context ) {
|
||||
public static String getIMSI(Context context) {
|
||||
|
||||
try {
|
||||
TelephonyManager telephonyManager = ( TelephonyManager ) context.getSystemService( Context.TELEPHONY_SERVICE );
|
||||
if ( telephonyManager != null &&
|
||||
ActivityCompat.checkSelfPermission( context, Manifest.permission.READ_PHONE_STATE ) == PackageManager.PERMISSION_GRANTED ) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager != null &&
|
||||
ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
|
||||
return telephonyManager.getSubscriberId();
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String checkSimState( Context context ) {
|
||||
public static String checkSimState(Context context) {
|
||||
|
||||
String mString = "";
|
||||
|
||||
if ( context == null ) {
|
||||
if (context == null) {
|
||||
return mString;
|
||||
}
|
||||
|
||||
TelephonyManager telephonyManager = ( TelephonyManager ) context.getSystemService( Context.TELEPHONY_SERVICE );
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
int simState = 0;
|
||||
if ( telephonyManager != null ) {
|
||||
if (telephonyManager != null) {
|
||||
simState = telephonyManager.getSimState();
|
||||
}
|
||||
|
||||
switch ( simState ) {
|
||||
switch (simState) {
|
||||
|
||||
case TelephonyManager.SIM_STATE_ABSENT:
|
||||
mString = "无卡";
|
||||
@@ -323,14 +328,14 @@ public class CommonUtils {
|
||||
/**
|
||||
* 获取路由器Mac
|
||||
*/
|
||||
public static String getRouterMac( Context context ) {
|
||||
public static String getRouterMac(Context context) {
|
||||
|
||||
if ( context == null ) {
|
||||
if (context == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
WifiManager wifi = ( WifiManager ) context.getApplicationContext().getSystemService( Context.WIFI_SERVICE );
|
||||
if ( wifi != null && wifi.getConnectionInfo() != null ) {
|
||||
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||
if (wifi != null && wifi.getConnectionInfo() != null) {
|
||||
return wifi.getConnectionInfo().getBSSID();
|
||||
}
|
||||
return "";
|
||||
@@ -339,50 +344,50 @@ public class CommonUtils {
|
||||
/**
|
||||
* 获取wifi名字
|
||||
*/
|
||||
public static String getWifiName( Context context ) {
|
||||
if ( context == null ) {
|
||||
public static String getWifiName(Context context) {
|
||||
if (context == null) {
|
||||
return "";
|
||||
}
|
||||
WifiManager wifi = ( WifiManager ) context.getApplicationContext().getSystemService( Context.WIFI_SERVICE );
|
||||
if ( wifi != null && wifi.getConnectionInfo() != null ) {
|
||||
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||
if (wifi != null && wifi.getConnectionInfo() != null) {
|
||||
return wifi.getConnectionInfo().getSSID();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
public static String getMobileIP( Context ctx ) {
|
||||
public static String getMobileIP(Context ctx) {
|
||||
|
||||
if ( ctx == null ) {
|
||||
if (ctx == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
ConnectivityManager mConnectivityManager = ( ConnectivityManager ) ctx.getSystemService( Context.CONNECTIVITY_SERVICE );// 获取系统的连接服务
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);// 获取系统的连接服务
|
||||
// 实例化mActiveNetInfo对象
|
||||
NetworkInfo mActiveNetInfo = null;// 获取网络连接的信息
|
||||
if ( mConnectivityManager != null ) {
|
||||
if (mConnectivityManager != null) {
|
||||
mActiveNetInfo = mConnectivityManager.getActiveNetworkInfo();
|
||||
}
|
||||
if ( mActiveNetInfo == null ) {
|
||||
if (mActiveNetInfo == null) {
|
||||
return "";
|
||||
} else {
|
||||
return getIp( mActiveNetInfo );
|
||||
return getIp(mActiveNetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
// 显示IP信息
|
||||
private static String getIp( NetworkInfo mActiveNetInfo ) {
|
||||
private static String getIp(NetworkInfo mActiveNetInfo) {
|
||||
|
||||
if ( mActiveNetInfo == null ) {
|
||||
if (mActiveNetInfo == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 如果是WIFI网络
|
||||
if ( mActiveNetInfo.getType() == ConnectivityManager.TYPE_WIFI ) {
|
||||
if (mActiveNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||
return getLocalIPAddress();
|
||||
}
|
||||
// 如果是手机网络
|
||||
else if ( mActiveNetInfo.getType() == ConnectivityManager.TYPE_MOBILE ) {
|
||||
else if (mActiveNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
|
||||
return getLocalIPAddress();
|
||||
} else {
|
||||
return "";
|
||||
@@ -393,16 +398,16 @@ public class CommonUtils {
|
||||
// 获取本地IP函数
|
||||
private static String getLocalIPAddress() {
|
||||
try {
|
||||
Enumeration< NetworkInterface > mEnumeration = NetworkInterface.getNetworkInterfaces();
|
||||
if ( mEnumeration != null ) {
|
||||
while ( mEnumeration.hasMoreElements() ) {
|
||||
Enumeration<NetworkInterface> mEnumeration = NetworkInterface.getNetworkInterfaces();
|
||||
if (mEnumeration != null) {
|
||||
while (mEnumeration.hasMoreElements()) {
|
||||
NetworkInterface intf = mEnumeration.nextElement();
|
||||
if ( intf != null && intf.getInetAddresses() != null ) {
|
||||
Enumeration< InetAddress > enumIPAddr = intf.getInetAddresses();
|
||||
while ( enumIPAddr.hasMoreElements() ) {
|
||||
if (intf != null && intf.getInetAddresses() != null) {
|
||||
Enumeration<InetAddress> enumIPAddr = intf.getInetAddresses();
|
||||
while (enumIPAddr.hasMoreElements()) {
|
||||
InetAddress inetAddress = enumIPAddr.nextElement();
|
||||
// 如果不是回环地址
|
||||
if ( inetAddress != null && !inetAddress.isLoopbackAddress() ) {
|
||||
if (inetAddress != null && !inetAddress.isLoopbackAddress()) {
|
||||
// 直接返回本地IP地址
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
@@ -410,124 +415,124 @@ public class CommonUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch ( SocketException ex ) {
|
||||
} catch (SocketException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getVersionName( Context context ) {
|
||||
return getVersionName( context, true );
|
||||
public static String getVersionName(Context context) {
|
||||
return getVersionName(context, true);
|
||||
}
|
||||
|
||||
public static String getVersionName( Context context, boolean fullVersionName ) {
|
||||
public static String getVersionName(Context context, boolean fullVersionName) {
|
||||
String appVersion = "";
|
||||
|
||||
try {
|
||||
String packageName = context.getApplicationInfo().packageName;
|
||||
appVersion = context.getPackageManager().getPackageInfo( packageName, 0 ).versionName;
|
||||
if ( !fullVersionName && appVersion != null && appVersion.length() > 0 ) {
|
||||
Matcher matcher = VERSION_NAME_PATTERN.matcher( appVersion );
|
||||
if ( matcher.matches() ) {
|
||||
appVersion = matcher.group( 1 );
|
||||
appVersion = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
|
||||
if (!fullVersionName && appVersion != null && appVersion.length() > 0) {
|
||||
Matcher matcher = VERSION_NAME_PATTERN.matcher(appVersion);
|
||||
if (matcher.matches()) {
|
||||
appVersion = matcher.group(1);
|
||||
}
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return appVersion;
|
||||
}
|
||||
|
||||
public static String getAppName( Context context ) {
|
||||
if ( context == null ) {
|
||||
public static String getAppName(Context context) {
|
||||
if (context == null) {
|
||||
return "";
|
||||
}
|
||||
PackageManager pm = context.getPackageManager();
|
||||
return context.getApplicationInfo().loadLabel( pm ).toString();
|
||||
return context.getApplicationInfo().loadLabel(pm).toString();
|
||||
|
||||
}
|
||||
|
||||
public static String getModel() {
|
||||
String temp = Build.MODEL;
|
||||
return TextUtils.isEmpty( temp ) ? "" : temp;
|
||||
return TextUtils.isEmpty(temp) ? "" : temp;
|
||||
}
|
||||
|
||||
@TargetApi( Build.VERSION_CODES.JELLY_BEAN )
|
||||
public static long getLeftMemory( Context context ) {
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
||||
public static long getLeftMemory(Context context) {
|
||||
|
||||
if ( context == null ) {
|
||||
if (context == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( Build.VERSION.SDK_INT >= 16 ) {
|
||||
ActivityManager mActivityManager = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
if (Build.VERSION.SDK_INT >= 16) {
|
||||
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
|
||||
mActivityManager.getMemoryInfo( mi );
|
||||
return ( mi.totalMem - mi.availMem ) / 1000;
|
||||
mActivityManager.getMemoryInfo(mi);
|
||||
return (mi.totalMem - mi.availMem) / 1000;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public static String encode( String string ) {
|
||||
public static String encode(String string) {
|
||||
try {
|
||||
return URLEncoder.encode( string, "UTF-8" );
|
||||
} catch ( UnsupportedEncodingException e ) {
|
||||
return URLEncoder.encode(string, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String decode( String string ) {
|
||||
public static String decode(String string) {
|
||||
try {
|
||||
return URLDecoder.decode( string, "UTF-8" );
|
||||
} catch ( UnsupportedEncodingException e ) {
|
||||
return URLDecoder.decode(string, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getProcessName( Context context, int pid ) {
|
||||
public static String getProcessName(Context context, int pid) {
|
||||
try {
|
||||
ActivityManager manager = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
for ( ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses() ) {
|
||||
if ( processInfo.pid == pid ) {
|
||||
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
|
||||
if (processInfo.pid == pid) {
|
||||
return processInfo.processName;
|
||||
}
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static int getStatusBarHeight( Context context ) {
|
||||
public static int getStatusBarHeight(Context context) {
|
||||
|
||||
if ( context == null ) {
|
||||
if (context == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int statusBarHeight = 0;
|
||||
try {
|
||||
Class c = Class.forName( "com.android.internal.R$dimen" );
|
||||
Class c = Class.forName("com.android.internal.R$dimen");
|
||||
Object obj = c.newInstance();
|
||||
Field field = c.getField( "status_bar_height" );
|
||||
int x = Integer.parseInt( field.get( obj ).toString() );
|
||||
statusBarHeight = context.getResources().getDimensionPixelSize( x );
|
||||
} catch ( Exception e ) {
|
||||
Field field = c.getField("status_bar_height");
|
||||
int x = Integer.parseInt(field.get(obj).toString());
|
||||
statusBarHeight = context.getResources().getDimensionPixelSize(x);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
if ( statusBarHeight > 0 ) {
|
||||
if (statusBarHeight > 0) {
|
||||
return statusBarHeight;
|
||||
}
|
||||
|
||||
try {
|
||||
int resourceId = context.getResources().getIdentifier( "status_bar_height", "dimen", "android" );
|
||||
if ( resourceId > 0 ) {
|
||||
statusBarHeight = context.getResources().getDimensionPixelSize( resourceId );
|
||||
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
@@ -541,13 +546,13 @@ public class CommonUtils {
|
||||
* @param packageName 包名,如果为空,将获取 context 本身的版本名称
|
||||
* @return
|
||||
*/
|
||||
public static String getVersionName( @NonNull Context context, @Nullable String packageName ) {
|
||||
public static String getVersionName(@NonNull Context context, @Nullable String packageName) {
|
||||
try {
|
||||
packageName = TextUtils.isEmpty( packageName ) ? context.getPackageName() : packageName;
|
||||
packageName = TextUtils.isEmpty(packageName) ? context.getPackageName() : packageName;
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo packInfo = packageManager.getPackageInfo( packageName, 0 );
|
||||
PackageInfo packInfo = packageManager.getPackageInfo(packageName, 0);
|
||||
return packInfo.versionName;
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -559,15 +564,124 @@ public class CommonUtils {
|
||||
* @param packageName 包名,如果为空,将获取 context 本身的版本号
|
||||
* @return
|
||||
*/
|
||||
public static int getVersionCode( @NonNull Context context, @Nullable String packageName ) {
|
||||
public static int getVersionCode(@NonNull Context context, @Nullable String packageName) {
|
||||
try {
|
||||
packageName = TextUtils.isEmpty( packageName ) ? context.getPackageName() : packageName;
|
||||
packageName = TextUtils.isEmpty(packageName) ? context.getPackageName() : packageName;
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo packInfo = packageManager.getPackageInfo( packageName, 0 );
|
||||
PackageInfo packInfo = packageManager.getPackageInfo(packageName, 0);
|
||||
return packInfo.versionCode;
|
||||
} catch ( Exception e ) {
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* cpu
|
||||
*/
|
||||
public static double getCPU(String packageName) {
|
||||
double Cpu = 0;
|
||||
try {
|
||||
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
Process proc = runtime.exec("adb shell top -n 1| grep " + packageName);
|
||||
try {
|
||||
if (proc.waitFor() != 0) {
|
||||
System.err.println("exit value = " + proc.exitValue());
|
||||
}
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
String line = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
stringBuffer.append(line + " ");
|
||||
|
||||
}
|
||||
|
||||
String str1 = stringBuffer.toString();
|
||||
String str3 = str1.substring(str1.indexOf(packageName) - 43, str1.indexOf(packageName)).trim();
|
||||
String cpu = str3.substring(0, 2);
|
||||
cpu = cpu.trim();
|
||||
Cpu = Double.parseDouble(cpu);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println(e);
|
||||
} finally {
|
||||
try {
|
||||
proc.destroy();
|
||||
} catch (Exception e2) {
|
||||
}
|
||||
}
|
||||
} catch (Exception StringIndexOutOfBoundsException) {
|
||||
|
||||
System.out.println("请检查设备是否连接");
|
||||
|
||||
}
|
||||
|
||||
return Cpu;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 电量
|
||||
*/
|
||||
public static float getBattery(Context context) {
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||
Intent batteryStatus = context.registerReceiver(null, filter);
|
||||
//当前剩余电量
|
||||
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
//电量最大值
|
||||
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
//电量百分比
|
||||
float batteryPct = level / (float) scale;
|
||||
return batteryPct;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内存占比
|
||||
*
|
||||
* @param packageName 包名
|
||||
* @return
|
||||
*/
|
||||
public static double getMemory(String packageName) {
|
||||
|
||||
double Heap = 0;
|
||||
|
||||
try {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
Process process = runtime.exec("adb shell dumpsys meminfo " + packageName);
|
||||
try {
|
||||
if (process.waitFor() != 0) {
|
||||
System.err.println("exit value = " + process.exitValue());
|
||||
}
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
String line = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
stringBuffer.append(line + " ");
|
||||
|
||||
}
|
||||
|
||||
String str1 = stringBuffer.toString();
|
||||
String str2 = str1.substring(str1.indexOf("Objects") - 60, str1.indexOf("Objects"));
|
||||
String str3 = str2.substring(0, 10);
|
||||
str3 = str3.trim();
|
||||
Heap = Double.parseDouble(str3) / 1024;
|
||||
DecimalFormat df = new DecimalFormat("#.000");
|
||||
String memory = df.format(Heap);
|
||||
Heap = Double.parseDouble(memory);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println(e);
|
||||
} finally {
|
||||
try {
|
||||
process.destroy();
|
||||
} catch (Exception e2) {
|
||||
}
|
||||
}
|
||||
} catch (Exception StringIndexOutOfBoundsException) {
|
||||
System.out.print("请检查设备是否连接");
|
||||
|
||||
}
|
||||
return Heap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user