Android 点击悬浮窗后台启动Activity问题及方案
短信预约 -IT技能 免费直播动态提醒
背景:开启悬浮窗,当app进入后台后,点击悬浮窗进入固定页面
问题:当app在后台运行时,点击悬浮窗,以下代码不能拉起app,经排查,部门手机需要开启后台弹出界面权限
val intent = Intent(context, PhoneMainActivity::class.java)intent.putExtra(MESSAGE_TYPE, pushMessageVO.type)intent.putExtra(JUMP, true)intent.flags = Intent.FLAG_ACTIVITY_NEW_TASKcontext.startActivity(intent)
如图
不同的机型这个权限的名称也不相同,要开启此权限,要跳转的界面也不同,所以需要获取不同机型的此页面路径
获取权限页面路径过程如下,测试机型:vivo X21A
获取连接设备名
adb devices
手机打开需要获取权限的页面,用以下命令获取路径
adb -s 设备名 shell dumpsys activity activities
如此,便结束了
附不同厂商跳转后台弹出界面,自启动,加入白名单,电池优化的工具类
import android.Manifest;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Build;import android.os.PowerManager;import android.provider.Settings;import android.util.Log;import androidx.annotation.IntRange;import androidx.annotation.RequiresApi;import androidx.core.app.ActivityCompat;import androidx.core.content.ContextCompat;import com.allqj.basic_lib.BasicLib;import java.util.Arrays;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;public class PermissionUtil { public static int IGNORE_BATTERY_CODE = 1880; private static LinkedHashMap> autoStartHashMap = new LinkedHashMap>() { { put("XIAOMI", Arrays.asList( "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity", "com.miui.securitycenter/com.miui.permcenter.autostart.AutoStartManagementActivity", "com.miui.securitycenter" )); put("HUAWEI", Arrays.asList( "com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity", "com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity", "com.huawei.systemmanager/.optimize.process.ProtectActivity", "com.huawei.systemmanager/.optimize.bootstart.BootStartActivity", "com.huawei.systemmanager" )); // 荣耀 put("HONOR", Arrays.asList( "com.hihonor.systemmanager/.appcontrol.activity.StartupAppControlActivity", "com.hihonor.systemmanager/.startupmgr.ui.StartupNormalAppListActivity", "com.hihonor.systemmanager" )); put("VIVO", Arrays.asList( "com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity", "com.vivo.permissionmanager/.activity.BgStartUpManagerActivity", "com.vivo.permissionmanager/.activity.PurviewTabActivity", "com.iqoo.secure/.ui.phoneoptimize.BgStartUpManager", "com.iqoo.secure/.safeguard.PurviewTabActivity",// "com.iqoo.secure/.ui.phoneoptimize.AddWhiteListActivity", //这是白名单, 不是自启动 "com.vivo.permissionmanager", "com.iqoo.secure" )); put("OPPO", Arrays.asList( "com.android.settings/com.oplus.settings.feature.homepage.OplusSettingsHomepageActivity", "com.android.settings/com.oplus.settings.OplusSubSettings", "com.android.settings/com.android.settings.SubSettings", "com.oplus.safecenter/.startupapp.view.StartupAppListActivity", "com.coloros.safecenter/.startupapp.StartupAppListActivity", "com.coloros.safecenter/.permission.startup.StartupAppListActivity", "com.oppo.safe/.permission.startup.StartupAppListActivity", "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity", "com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity", "com.coloros.safecenter", "com.oppo.safe", "com.coloros.oppoguardelf" )); put("MEIZU", Arrays.asList( "com.meizu.safe/.permission.SmartBGActivity", "com.meizu.safe/.permission.PermissionMainActivity", "com.meizu.safe" )); put("SAMSUNG", Arrays.asList( "com.samsung.android.sm_cn/com.samsung.android.sm.ui.ram.AutoRunActivity", "com.samsung.android.sm_cn/com.samsung.android.sm.ui.appmanagement.AppManagementActivity", "com.samsung.android.sm_cn/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity", "com.samsung.android.sm_cn/.ui.ram.RamActivity", "com.samsung.android.sm_cn/.app.dashboard.SmartManagerDashBoardActivity", "com.samsung.android.sm/com.samsung.android.sm.ui.ram.AutoRunActivity", "com.samsung.android.sm/com.samsung.android.sm.ui.appmanagement.AppManagementActivity", "com.samsung.android.sm/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity", "com.samsung.android.sm/.ui.ram.RamActivity", "com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity", "com.samsung.android.lool/com.samsung.android.sm.ui.battery.BatteryActivity", "com.samsung.android.sm_cn", "com.samsung.android.sm" )); put("ONEPLUS", Arrays.asList( "com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity", "com.oneplus.security" )); put("LETV", Arrays.asList( "com.letv.android.letvsafe/.AutobootManageActivity", "com.letv.android.letvsafe/.BackgroundAppManageActivity", "com.letv.android.letvsafe" )); put("ZTE", Arrays.asList( "com.zte.heartyservice/.autorun.AppAutoRunManager", "com.zte.heartyservice" )); //金立 put("F", Arrays.asList( "com.gionee.softmanager/.MainActivity", "com.gionee.softmanager" )); //以下为未确定(厂商名也不确定) put("SMARTISANOS", Arrays.asList( "com.smartisanos.security/.invokeHistory.InvokeHistoryActivity", "com.smartisanos.security" )); //360 put("360", Arrays.asList( "com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity", "com.yulong.android.coolsafe" )); //360 put("ULONG", Arrays.asList( "com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity", "com.yulong.android.coolsafe" )); //酷派 put("COOLPAD", Arrays.asList( "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain", "com.yulong.android.security" )); //联想 put("LENOVO", Arrays.asList( "com.lenovo.security/.purebackground.PureBackgroundActivity", "com.lenovo.security" )); put("HTC", Arrays.asList( "com.htc.pitroad/.landingpage.activity.LandingPageActivity", "com.htc.pitroad" )); //华硕 put("ASUS", Arrays.asList( "com.asus.mobilemanager/.MainActivity", "com.asus.mobilemanager" )); //酷派 put("YULONG", Arrays.asList( "com.yulong.android.softmanager/.SpeedupActivity", "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain", "com.yulong.android.security" )); } }; private static LinkedHashMap> bgPopHashMap = new LinkedHashMap>() { { put("XIAOMI", Arrays.asList( "com.miui.securitycenter/com.miui.permcenter.permissions.PermissionsEditorActivity", "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity", "com.miui.securitycenter" )); put("HUAWEI", Arrays.asList( "com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity", "com.huawei.systemmanager" )); // 荣耀 put("HONOR", Arrays.asList( "com.hihonor.systemmanager/.appcontrol.activity.StartupAppControlActivity", "com.hihonor.systemmanager/.startupmgr.ui.StartupNormalAppListActivity", "com.hihonor.systemmanager" )); put("VIVO", Arrays.asList( "com.vivo.permissionmanager/.activity.StartBgActivityControlActivity",//需要com.vivo.permission.manage.permission.ACCESS权限 "com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity", "com.vivo.permissionmanager/.activity.PurviewTabActivity", "com.vivo.permissionmanager", "com.iqoo.secure" )); put("MEIZU", Arrays.asList( "com.meizu.safe/.permission.PermissionMainActivity", "com.meizu.safe" )); put("OPPO", Arrays.asList( "com.oplus.battery/com.oplus.powermanager.fuelgaue.PowerControlActivity",//not exported "com.android.settings/com.oplus.settings.OplusSubSettings", "com.android.settings/com.android.settings.SubSettings", "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity", "com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity", "com.coloros.safecenter", "com.oppo.safe", "com.coloros.oppoguardelf" )); put("SAMSUNG", Arrays.asList( "com.samsung.android.lool", "com.samsung.android.sm_cn", "com.samsung.android.sm" )); put("ONEPLUS", Arrays.asList( "com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity", "com.oneplus.security" )); put("LETV", Arrays.asList( "com.letv.android.letvsafe/.AutobootManageActivity", "com.letv.android.letvsafe/.BackgroundAppManageActivity", "com.letv.android.letvsafe" )); put("ZTE", Arrays.asList( "com.zte.heartyservice" )); //金立 put("F", Arrays.asList( "com.gionee.softmanager/.MainActivity", "com.gionee.softmanager" )); //以下为未确定(厂商名也不确定) put("SMARTISANOS", Arrays.asList( "com.smartisanos.security/.invokeHistory.InvokeHistoryActivity", "com.smartisanos.security" )); //360 put("360", Arrays.asList( "com.yulong.android.coolsafe" )); //360 put("ULONG", Arrays.asList( "com.yulong.android.coolsafe" )); //酷派 put("COOLPAD", Arrays.asList( "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain", "com.yulong.android.security" )); //联想 put("LENOVO", Arrays.asList( "com.lenovo.security/.purebackground.PureBackgroundActivity", "com.lenovo.security" )); put("HTC", Arrays.asList( "com.htc.pitroad/.landingpage.activity.LandingPageActivity", "com.htc.pitroad" )); //华硕 put("ASUS", Arrays.asList( "com.asus.mobilemanager/.MainActivity", "com.asus.mobilemanager" )); //酷派 put("YULONG", Arrays.asList( "com.yulong.android.softmanager/.SpeedupActivity", "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain", "com.yulong.android.security" )); } }; private static LinkedHashMap> powerSavingHashMap = new LinkedHashMap>() { { put("XIAOMI", Arrays.asList( "com.miui.powerkeeper/.ui.HiddenAppsConfigActivity", "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity", "com.miui.powerkeeper", "com.miui.securitycenter" )); put("HUAWEI", Arrays.asList( "com.android.settings/.Settings$HighPowerApplicationsActivity", "com.android.settings" )); // 荣耀 put("HONOR", Arrays.asList( "com.hihonor.systemmanager/.power.ui.HwPowerManagerActivity", "com.hihonor.systemmanager" )); put("VIVO", Arrays.asList( "com.vivo.abe/com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity", "com.iqoo.powersaving/.PowerSavingManagerActivity", "com.iqoo.powersaving" )); put("OPPO", Arrays.asList( "com.oplus.battery/com.oplus.powermanager.fuelgaue.PowerControlActivity",//not exported "com.android.settings/com.oplus.settings.OplusSubSettings", "com.android.settings/com.android.settings.SubSettings", "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerConsumptionActivity", "com.coloros.oppoguardelf" )); } }; public static void startToPermissionSetting(Context context, String type) { Log.d("Util", "******************当前手机型号为:" + Build.MANUFACTURER); LinkedHashMap> permissionMap = new LinkedHashMap<>(); if ("autoStart".equals(type)) { permissionMap = autoStartHashMap; } else if ("powerSaving".equals(type)) { permissionMap = powerSavingHashMap; } else if ("bgPop".equals(type)) { permissionMap = bgPopHashMap; } if (!permissionMap.keySet().contains(Build.MANUFACTURER.toUpperCase())){ Intent intent = new Intent(Settings.ACTION_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); return; } for (Map.Entry> entry : permissionMap.entrySet()) { String manufacturer = entry.getKey(); List actCompatList = entry.getValue(); if (Build.MANUFACTURER.equalsIgnoreCase(manufacturer)) { for (String act : actCompatList) { try { Intent intent; if (act.contains("/")) {Log.d("Util", "******************act为:" + act);intent = new Intent();intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);ComponentName componentName = ComponentName.unflattenFromString(act);intent.setComponent(componentName);intent.putExtra("packagename", context.getPackageName());//vivo-SoftPermissionDetailActivityintent.putExtra("extra_pkgname", context.getPackageName());//xiaomi-PermissionsEditorActivityintent.putExtra("package_name", context.getPackageName());//xiaomi-ApplicationsDetailsActivity } else {//所以我是直接跳转到对应的安全管家/安全中心intent = context.getPackageManager().getLaunchIntentForPackage(act); } context.startActivity(intent); break; } catch (Exception e) { e.printStackTrace(); Intent intent = new Intent(Settings.ACTION_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } } } } public static boolean isIgnoringBatteryOptimizations(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powerManager != null) { return powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); } else { return false; } } else { return true; } } @RequiresApi(api = Build.VERSION_CODES.M) public static void requestIgnoreBatteryOptimizations(Activity context) { try { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + context.getPackageName())); context.startActivityForResult(intent, IGNORE_BATTERY_CODE); } catch (Exception e) { e.printStackTrace(); } }}
来源地址:https://blog.csdn.net/weixin_42068745/article/details/129816224
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341