Android

Android 取得手機內已安裝的APP資訊

1.先撈取APP所有的PackgeName

val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val resolveInfoList = packageManager.queryIntentActivities(intent, 0)

2.把所有app的資訊撈出來

for (resolveInfo in resolveInfoList) {
    val appPackageName = resolveInfo.activityInfo.applicationInfo.packageName
    if (resolveInfo.activityInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
         Log.d("appInfo","appPackageName\t{getAppName(appPackageName)}\t{getFirstInstalled(appPackageName)}\t{getLastUpdated(appPackageName)}\t{getAppVersion(appPackageName)}\t{getAppIconURI(appPackageName)}" )
    }
}

3.取得APP名稱

private fun getAppName(ApkPackageName: String) = packageManager.getApplicationLabel(
    packageManager.getApplicationInfo(
        ApkPackageName,
        0
    )
) as String

4.取得APP版本

private fun getAppVersion(ApkPackageName: String) = packageManager.getPackageInfo(ApkPackageName, 0).versionName

5.取得安裝的時間

private fun getFirstInstalled(ApkPackageName: String) = "安裝:${getDate(packageManager.getPackageInfo(ApkPackageName, 0).firstInstallTime)}"

6.取得最後更新的時間

private fun getLastUpdated(ApkPackageName: String) =  "最後更新:${getDate(packageManager.getPackageInfo(ApkPackageName, 0).lastUpdateTime)}"

7.時間轉換為西元年

@SuppressLint("SimpleDateFormat")
private fun getDate(milliSeconds: Long): String {
   val formatter = SimpleDateFormat("yyyy/MM/dd")
   val calendar = Calendar.getInstance()
   calendar.timeInMillis = milliSeconds
   return formatter.format(calendar.time)
}

8.取得APP ICON URI

private fun getAppIconURI(ApkTempPackageName: String): Uri {
   var resUri = Uri.EMPTY
   val appInfo = packageManager.getApplicationInfo(ApkTempPackageName, 0)
   if (appInfo.icon != 0) {
       resUri = Uri.parse("android.resource://" + ApkTempPackageName + "/" + appInfo.icon)
   }
        return resUri
}

9.取得結果

發表迴響