step1:
package com.mondor.myapplication;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class NotificationEffectActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_effect);
try {
PackageManager manager = getPackageManager();
PackageInfo packageInfo = manager.getPackageInfo(getPackageName(), 0);
AppInfo mAppInfo = AppManagerUtils.getAppInfo(manager, packageInfo);
Log.e("TAG", "uid_data: " + mAppInfo);
} catch (Exception e) {
e.printStackTrace();
}
String name = AppUtils.getAppName(NotificationEffectActivity.this);
String packageName = AppUtils.getPackageName(NotificationEffectActivity.this);
int uid = AppUtils.getUid(NotificationEffectActivity.this, packageName);
Log.e("TAG", "uid_data: " + name + "\t" + packageName + "\t" + uid);
}
}
step2:
package com.mondor.myapplication;
import android.graphics.drawable.Drawable;
public class AppInfo {
public long memSize;
public String appPackageName;
public String appPath;
public String appName;
public long appSize;
public Drawable appIcon;
public boolean isSD;
public boolean isSystem;
public boolean isCheck = false;
public boolean isVirus = false;
public String virusDesc = "安全";
public int scanProgress = 0;
public long cacheSize = 0;
public long fluxSize = -1;
public int uid = 0;
@Override
public String toString() {
return "AppInfo [memSize=" + memSize + ", appName=" + appName + "]";
}
@Override
public int hashCode() {
if (isSystem) {
return 1;
}
return -1;
}
@Override
public boolean equals(Object obj) {
AppInfo info = (AppInfo) obj;
if (info.isSystem) {
return true;
}
return false;
}
}
step3:
package com.mondor.myapplication;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
public class AppUtils {
public static synchronized String getAppName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static synchronized String getVersionName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static synchronized int getVersionCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static synchronized String getPackageName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.packageName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static synchronized Bitmap getBitmap(Context context) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = context.getApplicationContext()
.getPackageManager();
applicationInfo = packageManager.getApplicationInfo(
context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
Drawable d = packageManager.getApplicationIcon(applicationInfo);
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
return bm;
}
public static synchronized int getUid(Context context, String packageName) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
int uid = applicationInfo.uid;
return uid;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
step4:
package com.mondor.myapplication;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.Environment;
public class AppManagerUtils {
public static List<AppInfo> getAppInfo(Context context) {
List<AppInfo> list = new ArrayList<AppInfo>();
PackageManager manager = context.getPackageManager();
List<PackageInfo> packages = manager.getInstalledPackages(0);
for (PackageInfo packageInfo : packages) {
AppInfo info = getAppInfo(manager, packageInfo);
list.add(info);
}
return list;
}
public static AppInfo getAppInfo(PackageManager manager, PackageInfo packageInfo) {
AppInfo info = new AppInfo();
info.appPackageName = packageInfo.packageName;
ApplicationInfo applicationinfo = packageInfo.applicationInfo;
String appName = applicationinfo.loadLabel(manager).toString();
info.appName = appName;
int uid = applicationinfo.uid;
info.uid = uid;
long uidRxBytes = TrafficStats.getUidRxBytes(uid);
long uidTxBytes = TrafficStats.getUidTxBytes(uid);
long flux = uidRxBytes + uidTxBytes;
if (flux > 0) {
info.fluxSize = flux;
}
info.appPath = applicationinfo.sourceDir;
Drawable loadIcon = applicationinfo.loadIcon(manager);
info.appIcon = loadIcon;
String sourceDir = applicationinfo.sourceDir;
File file = new File(sourceDir);
long appSize = file.length();
info.appSize = appSize;
int flags = applicationinfo.flags;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
info.isSystem = true;
} else {
info.isSystem = false;
}
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
info.isSD = true;
} else {
info.isSD = false;
}
return info;
}
public static long getRomTotalSpace() {
File dataDirectory = Environment.getDataDirectory();
return dataDirectory.getTotalSpace();
}
public static long getRomFreeSpace() {
File dataDirectory = Environment.getDataDirectory();
return dataDirectory.getFreeSpace();
}
public static long getRomUsedSpace() {
return getRomTotalSpace() - getRomFreeSpace();
}
public static long getSDUsedSpace() {
return getSDTotalSpace() - getSDFreeSpace();
}
public static long getSDTotalSpace() {
File sdDirectory = Environment.getExternalStorageDirectory();
return sdDirectory.getTotalSpace();
}
public static long getSDFreeSpace() {
File sdDirectory = Environment.getExternalStorageDirectory();
return sdDirectory.getFreeSpace();
}
}
end
|