美文网首页
Android 获取APP 文件目录 & 模拟器检测

Android 获取APP 文件目录 & 模拟器检测

作者: 乘风破浪的程序员 | 来源:发表于2020-03-16 10:24 被阅读0次

1. 获取app 目录

context.getFilesDir()
Android 6.0的分身应用为了能与原应用区分开来,会更改手机的UserId,默认手机的UserId为0。

  1. 获取到的App应用位置数据在各Android API下的取值如下:

API 27 /data/user/0/packageName/files
API 26 /data/user/0/packageName/files
API 25 /data/user/0/packageName/files
API 24 /data/user/0/packageName/files
API 23 /data/user/0/packageName/files
API 22 /data/data/packageName/files
API 21 /data/data/packageName/files
API 19 /data/data/packageName/files
API 18 /data/data/packageName/files
API 17 /data/data/packageName/files
API 16 /data/data/packageName/files

But

华为Meta30 5G TAS-AN00 Android 10获取到的路径为:

"/data/user/10/packageName/files"

黑莓手机获取到的非上面标准路径;
雷蛇手机Android 8.1, 型号phone获取到的路径:

"/mnt/expand/60feb304-19a3-4847-ab3e-afeb626464fc/user/0/packageName/files"

华为平板:

/mnt/expand/50c89130-05ab-41f4-a0d1-27cf1eaee743/user/0/ packageName/files

image.png

2. 模拟器检测

  1. 通过光感来判断是否为模拟器:
    不可行, 原因是许多真实设备获取光感时返回为null
  2. 通过获取当前CPU:
    debug 时可检测出 mumu , release 包获取CPU信息时,检测失败。
    检测方法如下:
/**
     * 根据cpu 判断是否为电脑来检测模拟器
     */
    private fun checkIsNotRealPhone(): Boolean {
        val cpuInfo = readCpuInfo()
        return cpuInfo.contains("intel") || cpuInfo.contains("amd")
    }

    /**
     * 根据CPU是否为电脑来判断是否为模拟器(子方法)
     */
    private fun readCpuInfo(): String {
        var result = ""
        try {
            val args = arrayOf(
                "/system/bin/cat",
                "/proc/cpuinfo"
            )
            val cmd = ProcessBuilder(*args)
            val process = cmd.start()
            val sb = StringBuffer()
            var readLine: String? = ""
            val responseReader = BufferedReader(InputStreamReader(process.inputStream, "utf-8"))
            while (responseReader.readLine().also { readLine = it } !=
                null
            ) {
                sb.append(readLine)
            }
            responseReader.close()
            result = sb.toString().toLowerCase(Locale.getDefault())
        } catch (ex: IOException) {
        }
        return result
    }

debug 包获取到的CPU信息:

processor   : 0vendor_id    : genuineintelcpu family    : 6model        : 142model name : intel(r) core(tm) i7-8550u cpu @ 1.80ghzstepping  : 10cpu mhz     : 2400.000cache size    : 8192 kbphysical id    : 0siblings : 4core id      : 0cpu cores    : 4apicid       : 0initial apicid   : 0fdiv_bug : nof00f_bug    : nocoma_bug    : nofpu     : yesfpu_exception  : yescpuid level    : 22wp      : yesflags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch rdseed clflushoptbugs      :bogomips   : 3982.03clflush size   : 64cache_alignment : 64address sizes   : 39 bits physical, 48 bits virtualpower management:processor   : 1vendor_id    : genuineintelcpu family    : 6model        : 142model name : intel(r) core(tm) i7-8550u cpu @ 1.80ghzstepping  : 10cpu mhz     : 2400.000cache size    : 8192 kbphysical id    : 0siblings : 4core id      : 1cpu cores    : 4apicid       : 1initial apicid   : 1fdiv_bug : nof00f_bug    : nocoma_bug    : nofpu     : yesfpu_exception  : yescpuid level    : 22wp      : yesflags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch rdseed clflushoptbugs      :bogomips   : 3982.03clflush size   : 64cache_alignment : 64address sizes   : 39 bits physical, 48 bits virtualpower management:processor   : 2vendor_id    : genuineintelcpu family    : 6model        : 142model name : intel(r) core(tm) i7-8550u cpu @ 1.80ghzstepping  : 10cpu mhz     : 2400.000cache size    : 8192 kbphysical id    : 0siblings : 4core id      : 2cpu cores    : 4apicid       : 2initial apicid   : 2fdiv_bug : nof00f_bug    : nocoma_bug    : nofpu     : yesfpu_exception  : yescpuid level    : 22wp      : yesflags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch rdseed clflushoptbugs      :bogomips   : 3982.03clflush size   : 64cache_alignment : 64address sizes   : 39 bits physical, 48 bits virtualpower management:processor   : 3vendor_id    : genuineintelcpu family    : 6model        : 142model name : intel(r) core(tm) i7-8550u cpu @ 1.80ghzstepping  : 10cpu mhz     : 2400.000cache size    : 8192 kbphysical id    : 0siblings : 4core id      : 3cpu cores    : 4apicid       : 3initial apicid   : 3fdiv_bug : nof00f_bug    : nocoma_bug    : nofpu     : yesfpu_exception  : yescpuid level    : 22wp      : yesflags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch rdseed clflushoptbugs      :bogomips   : 3982.03clflush size   : 64cache_alignment : 64address sizes   : 39 bits physical, 48 bits virtualpower management:

release 包:

CPUInfo: processor  : armv7 processor rev 10 (v7l)bogomips  : 2387.98features   : neon vfp swp half thumb fastmult edsp vfpv3 vfpv4 idiva idivtcpu implementer  : 0x41cpu architecture: 7cpu variant    : 0x2cpu part   : 0xc09cpu revision : 10hardware    : omap4 panda boardrevision : 0020serial        : 0000000000000000

奇怪,猜测 debug 包和 release 包运行环境不同?

  1. 蓝牙名称:
    不准确,mumu 模拟器上可获取到蓝牙名称

mumu 模拟器:可以检测 Build.MODEL 为 MuMu

  1. https://cloud.tencent.com/developer/article/1431271

相关文章

网友评论

      本文标题:Android 获取APP 文件目录 & 模拟器检测

      本文链接:https://www.haomeiwen.com/subject/bkbwehtx.html