獲取Android開機(jī)啟動(dòng)列表
/**
* 獲取Android開機(jī)啟動(dòng)列表
*/
private static final String RECEIVE_BOOT_COMPLETED = "android.permission.RECEIVE_BOOT_COMPLETED";
private static final String ACTION_BOOT_COMPLETED = "android.permission.ACTION_BOOT_COMPLETED";
public static List<AutoStartInfo> fetchInstalledApps(Context mContext) {
PackageManager pm = mContext.getPackageManager();
List<ApplicationInfo> appInfo = pm.getInstalledApplications(0);
Iterator<ApplicationInfo> appInfoIterator = appInfo.iterator();
List<AutoStartInfo> appList = new ArrayList<AutoStartInfo>(
appInfo.size());
while (appInfoIterator.hasNext()) {
ApplicationInfo app = appInfoIterator.next();
int flag = pm.checkPermission(RECEIVE_BOOT_COMPLETED,
app.packageName);
if (flag == PackageManager.PERMISSION_GRANTED) {
AutoStartInfo appMap = new AutoStartInfo();
String label = pm.getApplicationLabel(app).toString();
Drawable icon = pm.getApplicationIcon(app);
String packageName = app.packageName;
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
appMap.setSystem(true);
// abAppProcessInfo.isSystem = true;
} else {
appMap.setSystem(false);
// abAppProcessInfo.isSystem = false;
}
// appMap.setDesc(desc);
appMap.setIcon(icon);
appMap.setPackageName(packageName);
appMap.setLabel(label);
appList.add(appMap);
}
}
return appList;
}
獲取自啟應(yīng)用
/**
* 獲取自啟應(yīng)用
*
* @param mContext
* @return
*/
public static List<AutoStartInfo> fetchAutoApps(Context mContext) {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED);
List<ResolveInfo> resolveInfoList = pm.queryBroadcastReceivers(intent,
PackageManager.GET_DISABLED_COMPONENTS);
List<AutoStartInfo> appList = new ArrayList<AutoStartInfo>();
// 得到的參數(shù)
String appName = null;
String packageReceiver = null;
Drawable icon = null;
boolean isSystem = false;
boolean isenable = true;
String packageName = null;
boolean isAutoStart = false;
boolean isBackStart = false;
/**
* 通過 PackageInfo 獲取具體信息方法:
*
* 包名獲取方法:packageInfo.packageName
* icon獲取獲取方法:packageManager.getApplicationIcon(applicationInfo)
* 應(yīng)用名稱獲取方法:packageManager.getApplicationLabel(applicationInfo)
* 使用權(quán)限獲取方法:packageManager.getPackageInfo(packageName,PackageManager.
* GET_PERMISSIONS) .requestedPermissions
*
* 通過 ResolveInfo 獲取具體信息方法:
*
* 包名獲取方法:resolve.activityInfo.packageName
* icon獲取獲取方法:resolve.loadIcon(packageManager)
* 應(yīng)用名稱獲取方法:resolve.loadLabel(packageManager).toString()
*
*/
if (resolveInfoList.size() > 0) {
for (int i = 0; resolveInfoList.size() > i; i++) {
isAutoStart = false;
isBackStart = false;
/**
* // 查找安裝的package是否有開機(jī)啟動(dòng)權(quán)限 if(PackageManager.PERMISSION_GRANTED
* == context
* .getPackageManager().checkPermission(BOOT_START_PERMISSION,
* app.packageName))
*
* BOOT_COMPLETED BOOT_START_PERMISSION RECEIVE_BOOT_COMPLETED
* ACTION_BOOT_COMPLETED
*/
if (mContext.getPackageManager().checkPermission(
RECEIVE_BOOT_COMPLETED,
resolveInfoList.get(i).activityInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
isAutoStart = true;
}
if (mContext.getPackageManager().checkPermission(
ACTION_BOOT_COMPLETED,
resolveInfoList.get(i).activityInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
isBackStart = true;
}
appName = resolveInfoList.get(i).loadLabel(pm).toString();
packageName = resolveInfoList.get(i).activityInfo.packageName;
packageReceiver = resolveInfoList.get(i).activityInfo.packageName
+ "/" + resolveInfoList.get(i).activityInfo.name;
icon = resolveInfoList.get(i).loadIcon(pm);
ComponentName mComponentName2 = new ComponentName(
resolveInfoList.get(i).activityInfo.packageName,
resolveInfoList.get(i).activityInfo.name);
if (pm.getComponentEnabledSetting(mComponentName2) == 2) {
isenable = false;
} else {
isenable = true;
}
if ((resolveInfoList.get(i).activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
isSystem = true;
} else {
isSystem = false;
}
//
AutoStartInfo mAutoStartInfo = new AutoStartInfo();
mAutoStartInfo.setLabel(appName);
mAutoStartInfo.setSystem(isSystem);
mAutoStartInfo.setEnable(isenable);
mAutoStartInfo.setIcon(icon);
mAutoStartInfo.setPackageName(packageName);
mAutoStartInfo.setPackageReceiver(packageReceiver);
mAutoStartInfo.setAutoStart(isAutoStart);
mAutoStartInfo.setBackStart(isBackStart);
boolean isAdd = true;
if (appList != null) {
for (int j = 0; appList.size() > j; j++) {
if (appList.get(j).getPackageName().equals(packageName)) {
isAdd = false;
}
}
}
if (isAdd) {
appList.add(mAutoStartInfo);
}
}
}
return appList;
}
單位轉(zhuǎn)換
/**
* 單位轉(zhuǎn)換
*
* @param length
* @return
*/
public static String toSize(double length) {
long kb = 1024;
long mb = 1024 * kb;
long gb = 1024 * mb;
if (length < kb) {
return String.format("%d B", (int) length);
} else if (length < mb) {
return String.format("%.2f KB", length / kb);
} else if (length < gb) {
return String.format("%.2f MB", length / mb);
} else {
return String.format("%.2f GB", length / gb);
}
}
// storage, G M K B
public static String convertStorage(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
return String.format("%d B", size);
}
public static StorageSize convertStorageSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
StorageSize sto = new StorageSize();
if (size >= gb) {
sto.suffix = "GB";
sto.value = (float) size / gb;
return sto;
} else if (size >= mb) {
sto.suffix = "MB";
sto.value = (float) size / mb;
return sto;
} else if (size >= kb) {
sto.suffix = "KB";
sto.value = (float) size / kb;
return sto;
} else {
sto.suffix = "B";
sto.value = (float) size;
return sto;
}
}
獲取SD卡信息
public static SDCardInfo getSDCardInfo() {
// String sDcString = Environment.getExternalStorageState();
if (Environment.isExternalStorageRemovable()) {
String sDcString = Environment.getExternalStorageState();
if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
File pathFile = Environment.getExternalStorageDirectory();
try {
StatFs statfs = new StatFs(pathFile.getPath());
// 獲取SDCard上BLOCK總數(shù)
long nTotalBlocks = statfs.getBlockCount();
// 獲取SDCard上每個(gè)block的SIZE
long nBlocSize = statfs.getBlockSize();
// 獲取可供程序使用的Block的數(shù)量
long nAvailaBlock = statfs.getAvailableBlocks();
// 獲取剩下的所有Block的數(shù)量(包括預(yù)留的一般程序無法使用的塊)
long nFreeBlock = statfs.getFreeBlocks();
SDCardInfo info = new SDCardInfo();
// 計(jì)算SDCard 總?cè)萘看笮B
info.total = nTotalBlocks * nBlocSize;
// 計(jì)算 SDCard 剩余大小MB
info.free = nAvailaBlock * nBlocSize;
return info;
} catch (IllegalArgumentException e) {
}
}
}
return null;
}
/**
* data 目錄 getDataDirectory()
*
* @param context
* @return
*/
public static SDCardInfo getSystemSpaceInfo(Context context) {
File path = Environment.getDataDirectory();
// File path = context.getCacheDir().getAbsoluteFile();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize * totalBlocks;
long availSize = availableBlocks * blockSize;
SDCardInfo info = new SDCardInfo();
info.total = totalSize;
info.free = availSize;
return info;
}
public static SDCardInfo getRootSpaceInfo() {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize * totalBlocks;
long availSize = availableBlocks * blockSize;
// 獲取SDCard上每個(gè)block的SIZE
long nBlocSize = stat.getBlockSize();
SDCardInfo info = new SDCardInfo();
// 計(jì)算SDCard 總?cè)萘看笮B
info.total = totalSize;
// 計(jì)算 SDCard 剩余大小MB
info.free = availSize;
return info;
}
沉浸狀態(tài)欄
public void sethah(Window window) {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// 透明狀態(tài)欄
// getWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明導(dǎo)航欄
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
檢查當(dāng)前網(wǎng)絡(luò)是否可用
/**
* 檢查當(dāng)前網(wǎng)絡(luò)是否可用
*
* @param activity
* @return
*/
public boolean isNetworkAvailable(Activity activity) {
Context context = activity.getApplicationContext();
// 獲取手機(jī)所有連接管理對象(包括對Wifi,net等連接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// 獲取NetworkInfo對象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
System.out.println(i + "===state==="
+ networkInfo[i].getState());
System.out.println(i + "===style==="
+ networkInfo[i].getTypeName());
// 判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)是否為連接狀態(tài)
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
安裝APK
/**
* 安裝APK
*
* @param context
* @param packageName
*/
public void update(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), packageName)),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
判斷某個(gè)程序是否安裝在手機(jī)中
/**
* 判斷某個(gè)程序是否安裝在手機(jī)中
*
* @param context
* 上下文
* @param packageName
* 需要驗(yàn)證的包名
* @return 是否安裝
*/
public static boolean isPackageExist(Context context, String packageName) {
if (packageName == null)
return false;
boolean packageExist = false;
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo(packageName, 0);
packageExist = true;
} catch (PackageManager.NameNotFoundException ignored) {
// L.d("isPackageExist:" + ignored);
}
return packageExist;
}
判斷APK是否已安裝
/**
* 判斷APK是否已安裝
*
* @param pm
* @param packageName
* @param versionCode
* @return
*/
public int doType(PackageManager pm, String packageName, int versionCode) {
List<PackageInfo> pakageinfos = pm
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo pi : pakageinfos) {
String pi_packageName = pi.packageName;
int pi_versionCode = pi.versionCode;
// 如果這個(gè)包名在系統(tǒng)已經(jīng)安裝過的應(yīng)用中存在
if (packageName.endsWith(pi_packageName)) {
// Log.i("test","此應(yīng)用安裝過了");
if (versionCode == pi_versionCode) {
Log.i("test", "已經(jīng)安裝,不用更新,可以卸載該應(yīng)用");
return 1000;
} else if (versionCode > pi_versionCode) {
Log.i("test", "已經(jīng)安裝,有更新");
return 2000;
}
}
}
Log.i("test", "未安裝該應(yīng)用,可以安裝");
return 3000;
}
獲取當(dāng)前版本的版本名字
/**
* 獲取當(dāng)前版本的版本名字
*
* @param context
* @return
*/
public String getVersion(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
return context.getResources()
.getString(R.string.unknown_versionnum);
}
}
獲取當(dāng)前版本的版本號
/**
* 獲取當(dāng)前版本的版本號
*
* @return
*/
public int getVersiontheCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return R.string.unknown_versionnum;
}
}
判斷WIFI是否連接
/ **
* 判斷WIFI是否連接
*
* @param context
* @return true為已連接
*/
boolean isWifi = false;
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true;
}
return false;
}
刪除單個(gè)文件
` /**
* 刪除單個(gè)文件
*
* @param filePath
* 被刪除文件的文件名
* @return 文件刪除成功返回true,否則返回false
*/
public boolean deleteFile(String filePath, Context context) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
boolean isOK = file.delete();
scanFile(file, context);
return isOK;
} else {
Toast.makeText(context, "null", 0).show();
}
return false;
}`
掃描文件
/**
* 掃描文件
*
* @param file
* @param context
*/
public void scanFile(File file, Context context) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
}
/**
* get,set
*/
private List<APKInfo> myFiles = new ArrayList<APKInfo>();
public List<APKInfo> getMyFiles() {
return myFiles;
}
public void setMyFiles(List<APKInfo> myFiles) {
this.myFiles = myFiles;
}
運(yùn)用遞歸的思想,遞歸去找每個(gè)目錄下面的apk文件
/**
* 運(yùn)用遞歸的思想,遞歸去找每個(gè)目錄下面的apk文件
*
* @param file
*/
public void FindAllAPKFile(File file, Context context) {
// 手機(jī)上的文件,目前只判斷SD卡上的APK文件
// file = Environment.getDataDirectory();
// SD卡上的文件目錄
if (file.isFile()) {
String name_s = file.getName();
APKInfo myFile = new APKInfo();
String apk_path = null;
// MimeTypeMap.getSingleton()
if (name_s.toLowerCase().endsWith(".apk")) {
apk_path = file.getAbsolutePath();// apk文件的絕對路勁
// System.out.println("----" + file.getAbsolutePath() + "" +
// name_s);
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
/** 獲取apk的圖標(biāo) */
appInfo.sourceDir = apk_path;
appInfo.publicSourceDir = apk_path;
Drawable apk_icon = appInfo.loadIcon(pm);
myFile.setIcon(apk_icon);
/** 得到包名 */
String packageName = packageInfo.packageName;
myFile.setName(packageName);
/** apk的絕對路勁 */
myFile.setPath(file.getAbsolutePath());
/** apk的版本名稱 String */
String versionName = packageInfo.versionName;
myFile.setVersionName(versionName);
/** apk的版本號碼 int */
int versionCode = packageInfo.versionCode;
myFile.setVersionCode(versionCode);
/** 安裝處理類型 */
int type = doType(pm, packageName, versionCode);
myFile.setType(type);
Log.i("ok", "處理類型:" + String.valueOf(type) + "\n"
+ "------------------我是純潔的分割線-------------------");
myFiles.add(myFile);
}
// String apk_app = name_s.substring(name_s.lastIndexOf("."));
} else {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File file_str : files) {
FindAllAPKFile(file_str, context);
}
}
}
}
獲得所有的應(yīng)用程序信息
/**
* 獲得所有的應(yīng)用程序信息
*
* @return
*/
public static List<AppBean> getAllApps(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
List<AppBean> list = new ArrayList<AppBean>();
for (PackageInfo info : packages) {
ApplicationInfo applicationInfo = info.applicationInfo;
String name = applicationInfo.loadLabel(pm).toString();
Drawable icon = applicationInfo.loadIcon(pm);
String sourceDir = applicationInfo.sourceDir;
File file = new File(sourceDir);
int flags = applicationInfo.flags;
boolean isInstallSD = false;
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
isInstallSD = true;
}
boolean isSystem = false;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
isSystem = true;
}
AppBean bean = new AppBean();
bean.icon = icon;
bean.name = name;
bean.size = file.length();
bean.isInstallSD = isInstallSD;
bean.isSystem = isSystem;
bean.packageName = info.packageName;
list.add(bean);
}
return list;
}
public static List<AppBean> getAllLaunchApps(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
List<AppBean> list = new ArrayList<AppBean>();
for (PackageInfo info : packages) {
Intent intent = pm.getLaunchIntentForPackage(info.packageName);
if (intent == null) {
continue;
}
ApplicationInfo applicationInfo = info.applicationInfo;
String name = applicationInfo.loadLabel(pm).toString();
Drawable icon = applicationInfo.loadIcon(pm);
String sourceDir = applicationInfo.sourceDir;
File file = new File(sourceDir);
int flags = applicationInfo.flags;
boolean isInstallSD = false;
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
isInstallSD = true;
}
boolean isSystem = false;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
isSystem = true;
}
AppBean bean = new AppBean();
bean.icon = icon;
bean.name = name;
bean.size = file.length();
bean.isInstallSD = isInstallSD;
bean.isSystem = isSystem;
bean.packageName = info.packageName;
list.add(bean);
}
return list;
}