关于android 悬浮窗和自启动的设置, 以及获取系统的信息

关于android 悬浮窗和自启动的设置, 以及获取系统的信息

标签(空格分隔):Android


悬浮窗

对于是否有开悬浮窗,程序是可以检测到的。
权限声明:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

权限检查:

对于android6.0 以上的机型来说,google将悬浮窗权限和其他危险权限单独列出,因此可以检测, 方法:

fun checkFloatWindowPermission(): Boolean {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val result = Settings.canDrawOverlays(BaseContext.application)
        return result
    } else {
        //6.0 以下
        val result = checkRomFloatWindowPermission()
        return result
    }
    return true
}

android 4.4以下大部分机型声明即获取(包括大部分国产机), 因此只需判断4.4.4 -- 5.1之间的系统即可。

//如下,经过我的测试,只有小米和魅族, 华为需要在`api<=19`时单独处理,其他正常。
fun checkRomFloatWindowPermission(): Boolean {

    val brand = Build.MANUFACTURER
    if (brand.contains("Xiaomi")) {
        //check miui permission
        return checkMeizuAndMIUIFloatWindowPermission()
    }
    if (brand.contains("Huawei") || brand.contains("HUAWEI")) {
        return checkMeizuAndMIUIFloatWindowPermission()
    }
    if (Build.VERSION.SDK_INT <= 19) {
        return true
    }
    when (brand) {
        "Meizu" -> return checkMeizuAndMIUIFloatWindowPermission()
        "OPPO" -> return true
        "vivo" -> return true
        "Sony" -> return true
        "Letv" -> return true
        "LG" -> return true
    }
    return true
}
//-------------- Meizu MIUI huawei start---------------------

fun checkMeizuAndMIUIFloatWindowPermission(): Boolean {
    if (Build.VERSION.SDK_INT >= 19) {
        return checkMeizuAndMIUIOp(24)
    }
    return true
}

fun checkMeizuAndMIUIOp(op: Int): Boolean {
    if (Build.VERSION.SDK_INT >= 19) {
        val opsManager = BaseContext.application.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
        try {
            val clazz = AppOpsManager::class.java
            val method = clazz.getDeclaredMethod("checkOp",
                    Int::class.javaPrimitiveType, Int::class.javaPrimitiveType, String::class.java)
            return AppOpsManager.MODE_ALLOWED == method.invoke(opsManager, op, Binder.getCallingUid(), BaseContext.application.packageName)
        } catch (e: Exception) {
            Logger.d(TAG, "checkOp", e)
        }
    } else {
        Logger.d(TAG, "Below API 19 cannot invoke!")
    }
    return false
}

//-------------- Meizu MIUI end ---------------------

至此,权限检查完毕。
不能动态获取,只能跳转到这只页去手动开启。

fun applyFloatWindowPermission(context: Context) {
    if (Build.VERSION.SDK_INT >= 23) {
        applySystemPermission(context)
        return
    }
    val brand = Build.MANUFACTURER
    when (brand) {
        "Huawei", "HUAWEI" -> applyHuaweiPermission(context)
        "Xiaomi" -> applyMIUIPermSetting(context)
        "Meizu" -> applyMeizuPermission(context)
        "OPPO" -> openOppoPermSetting(context)
        "vivo" -> openVivoPermSetting(context)
        else -> applySystemPermission(context)
    }
}

//-------------- Sysytem start ---------------------

fun  applySystemPermission(activity: Activity) {
    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
    intent.data = Uri.parse("package:" + activity.packageName)
    activity.startActivityForResult(intent, 1)

}

//-------------- Sysytem end ---------------------
//-------------- Meizu start---------------------
fun applyMeizuPermission(activity: Activity) {
    val intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
    intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity")
    intent.putExtra("packageName", activity.packageName)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    activity.startActivity(intent)
}

//-------------- Meizu end ---------------------
//-------------- MIUI start---------------------
fun applyMIUIPermSetting(activity: Activity) {
    openMIUIPermSetting(activity)
}

fun openMIUIPermSetting(context: Context): Boolean {
    val i = Intent("miui.intent.action.APP_PERM_EDITOR")
    val componentName = ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity")
    i.component = componentName
    i.putExtra("extra_pkgname", context.packageName)
    try {
        context.startActivity(i)
    } catch (e: Exception) {
        Logger.e(TAG, "openMIUIPermSetting jump e:" + e.message)
        openSystemSetting(context)
        return false
    }
    return true
}
//-------------- MIUI start---------------------
//-------------- huawei start---------------------

fun applyHuaweiPermission(context: Context) {
    try {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        var comp = ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity")//悬浮窗管理页面
        intent.component = comp
        val version = getEmuiVersion()
        Logger.d(TAG, "applyHuaweiPermission:"  + version)
        if (version == 3.0) {
            comp = ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity")//悬浮窗管理页面
            intent.component = comp
            context.startActivity(intent)
            return
        }
        context.startActivity(intent)
//        if (version == 3.1) {
//            //emui 3.1 的适配
//            context.startActivity(intent)
//        } else {
//            //emui 3.0 的适配
//            comp = ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity")//悬浮窗管理页面
//            intent.component = comp
//            context.startActivity(intent)
//        }
    } catch (e: Exception) {
        //抛出异常时提示信息
        Toast.makeText(context, "进入设置页面失败,请手动设置", Toast.LENGTH_LONG).show()
        Log.e(TAG, Log.getStackTraceString(e))
    }
}

fun getEmuiVersion(): Double {
    try {
        val emuiVersion = getSystemProperty("ro.build.version.emui") ?: return 4.0
        val version = emuiVersion.substring(emuiVersion.indexOf("_") + 1)
        return java.lang.Double.parseDouble(version)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return 4.0
}
//-------------- huawei end---------------------

附: 各手机厂商跳转到设置页的方法:(context)

// open app setting according to rom ----------------

fun applyBrandStrategy(activity: Activity) {
    val brand = Build.MANUFACTURER
    when (brand) {
        "Huawei", "HUAWEI" -> openHuaweiPermSetting(activity)
        "Xiaomi" -> openMIUIPermSetting(activity)
        "Meizu" -> openMeizuPermSetting(activity)
        "OPPO" -> openOppoPermSetting(activity)
        "vivo" -> openVivoPermSetting(activity)
        "Sony" -> openSonyPermSetting(activity)
        "Letv" -> openLetvPermSetting(activity)
        "LG" -> openLGPermSetting(activity)
        else -> openSystemSetting(activity)
    }
}

fun openMIUIPermSetting(activity: Activity): Boolean {
    val i = Intent("miui.intent.action.APP_PERM_EDITOR")
    val componentName = ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity")
    i.component = componentName
    i.putExtra("extra_pkgname", activity.packageName)
    try {
        activity.startActivity(i)
    } catch (e: Exception) {
        Logger.e(TAG, "openMIUIPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openMeizuPermSetting(activity: Activity): Boolean {
    val intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
    intent.addCategory(Intent.CATEGORY_DEFAULT)
    intent.putExtra("packageName", activity.packageName)
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openMeizuPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openHuaweiPermSetting(activity: Activity): Boolean {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    var comp = ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openHuaweiPermSetting jump e:" + e.message)
        comp = ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.SingleAppActivity")
        intent.component = comp
        try {
            activity.startActivity(intent)
        } catch (e1: Exception) {
            Logger.e(TAG, "openHuaweiPermSetting jump e1:" + e.message)
            openSystemSetting(activity)
            return false
        }
    }

    return true
}

fun openOppoPermSetting(activity: Activity): Boolean {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    val comp = ComponentName("com.oppo.safe", "com.oppo.safe.permission.PermissionSettingsActivity")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openOppoPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openVivoPermSetting(activity: Activity): Boolean {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    val comp = ComponentName("com.iqoo.secure", "com.iqoo.secure.MainActivity")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openVivoPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openSonyPermSetting(activity: Activity): Boolean {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    val comp = ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openSonyPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openLGPermSetting(activity: Activity): Boolean {
    val intent = Intent("android.intent.action.MAIN")
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    val comp = ComponentName("com.android.settings", "com.android.settings.Settings\$AccessLockSummaryActivity")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openLGPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openLetvPermSetting(activity: Activity): Boolean {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra("packageName", activity.packageName)
    val comp = ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.PermissionAndApps")
    intent.component = comp
    try {
        activity.startActivity(intent)
    } catch (e: Exception) {
        Logger.e(TAG, "openLetvPermSetting jump e:" + e.message)
        openSystemSetting(activity)
        return false
    }

    return true
}

fun openSystemSetting(activity: Activity): Boolean {
    val localIntent = Intent()
    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
    localIntent.data = Uri.fromParts("package", activity.packageName, null)
    try {
        activity.startActivity(localIntent)
    } catch (e: Exception) {
        Logger.e(TAG, "openSystemSetting jump e:" + e.message)
        return false
    }

    return true
}

开启自启动

自启动比较简单,android系统无法检测到是否开启,一般做法是首次安装提示用户跳转设置:

    private fun applySelfStartSetting() {

        var intent = Intent()
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            var componentName: ComponentName? = null
            val brand = Build.MANUFACTURER
            when (brand) {
                "Xiaomi" -> {
                    componentName = ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")
                }
                "samsung", "Samsung" -> {
                    componentName = ComponentName("com.samsung.android.sm" ,"com.samsung.android.sm.app.dashboard.SmartManagerDashBoardActivity")
                }
                "Huawei", "HUAWEI" -> {
                    componentName = ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")
                }
                "Meizu" -> {
                    componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity")
                }
                "OPPO" -> {
                    componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity")
                }
                "vivo" -> {
                    componentName = ComponentName.unflattenFromString("com.iqoo.secure/.safeguard.PurviewTabActivity")
                }
                "Letv" -> {
                    intent.action = "com.letv.android.permissionautoboot"
                }
                else -> {
                    intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                    intent.data = Uri.fromParts("package", packageName, null)
                }
            }
            intent.component = componentName
            startActivity(intent)
        } catch (e: Exception) {
            Logger.d(PhoneDialogActivity.TAG, "applySelfStartSetting:"  + e)
            intent = Intent(Settings.ACTION_SETTINGS)
            startActivity(intent)
        }
    }

android 相关信息的获取方法:

1.获取手机型号(Build)

所属包: android.os.Build
作用(含义): 从系统属性中提取设备硬件和版本信息
静态属性:

1. BOARD 主板:The name of the underlying board, like goldfish. 
2.  BOOTLOADER 系统启动程序版本号:The system bootloader version number. 
3. BRAND 系统定制商:The consumer-visible brand with which the product/hardware will be associated, if any. 
4. CPU_ABI cpu指令集:The name of the instruction set (CPU type + ABI convention) of native code. 
5. CPU_ABI2 cpu指令集2:The name of the second instruction set (CPU type + ABI convention) of native code. 
6. DEVICE 设备参数:The name of the industrial design. 
7. DISPLAY 显示屏参数:A build ID string meant for displaying to the user 
8. FINGERPRINT 唯一识别码:A string that uniquely identifies this build. Do not attempt to parse this value. 
9. HARDWARE 硬件名称:The name of the hardware (from the kernel command line or /proc). 
10. HOST 
11. ID 修订版本列表:Either a changelist number, or a label like M4-rc20. 
12. MANUFACTURER 硬件制造商:The manufacturer of the product/hardware.(我们目前只需要关注这个静态属性即可)
13. MODEL 版本即最终用户可见的名称:The end-user-visible name for the end product. 
14. PRODUCT 整个产品的名称:The name of the overall product. 
15. RADIO 无线电固件版本:The radio firmware version number. 在API14后已过时。使用 getRadioVersion()代替。 
16. SERIAL 硬件序列号:A hardware serial number, if available. Alphanumeric only, case-insensitive. 
17. TAGS 描述build的标签,如未签名,debug等等。:Comma-separated tags describing the build, like unsigned,debug. 
18. TIME 
19. TYPE build的类型:The type of build, like user or eng. 
20. USER

打开其他应用程序中的Activity或服务(ComponentName)

所属包: android.content.ComponentName
构造方法使用方式如下:

  1. 传递当前上下文和将要跳转的类名;
  2. 传递一个String包名和String类名;
  3. 传递一个Parcel数据容器。
    需要关注的方法:unflattenFromString(“传递将要跳转的地址,格式为包名/跳转Activity Name”)

通过adb获取跳转包名路径

adb为我们提供了一个可以打印出当前系统所有service信息,在后面可加上具体的服务名的命令

adb shell dumpsys

获取设备电池信息:adb shell dumpsys battery
获取cpu信息:adb shell dumpsys cpuinfo
获取内存信息:adb shell dumpsys meminfo 
要获取具体应用的内存信息,可加上包名 
adb shell dumpsys meminfo PACKAGE_NAME
获取Activity信息:adb shell dumpsys activity
获取package信息:adb shell dumpsys package 
加上-h可以获取帮助信息 
获取某个包的信息:adb shell dumpsys package PACKAGE_NAME
获取通知信息:adb shell dumpsys notification
获取wifi信息:adb shell dumpsys wifi 
可以获取到当前连接的wifi名、搜索到的wifi列表、wifi强度等
获取电源管理信息:adb shell dumpsys power 
可以获取到是否处于锁屏状态:mWakefulness=Asleep或者mScreenOn=false
获取电话信息:adb shell dumpsys telephony.registry 
可以获取到电话状态,例如mCallState值为0,表示待机状态、1表示来电未接听状态、2表示电话占线状态 
mCallForwarding=false #是否启用呼叫转移 
mDataConnectionState=2 #0:无数据连接 1:正在创建数据连接 2:已连接mDataConnectionPossible=true #是否有数据连接mDataConnectionApn= #APN名称等
adb shell dumpsys activity top
//获取当前显示activity的详细信息,包括所属包名和activity名,activity的布局信息等等。

参考资料 : http://www.voidcn.com/article/p-dpiicqfm-zw.html


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容