当前位置:   article > 正文

Android 点击悬浮窗后台启动Activity问题及方案_vivo后台启动activity

vivo后台启动activity

背景:开启悬浮窗,当app进入后台后,点击悬浮窗进入固定页面

问题:当app在后台运行时,点击悬浮窗,以下代码不能拉起app,经排查,部门手机需要开启后台弹出界面权限

  1. val intent = Intent(context, PhoneMainActivity::class.java)
  2. intent.putExtra(MESSAGE_TYPE, pushMessageVO.type)
  3. intent.putExtra(JUMP, true)
  4. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
  5. context.startActivity(intent)

如图 

不同的机型这个权限的名称也不相同,要开启此权限,要跳转的界面也不同,所以需要获取不同机型的此页面路径

获取权限页面路径过程如下,测试机型:vivo X21A

1. 获取连接设备名

adb devices

2. 手机打开需要获取权限的页面,用以下命令获取路径

adb -s 设备名 shell dumpsys activity activities

 如此,便结束了

附不同厂商跳转后台弹出界面,自启动,加入白名单,电池优化的工具类

  1. import android.Manifest;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.os.PowerManager;
  10. import android.provider.Settings;
  11. import android.util.Log;
  12. import androidx.annotation.IntRange;
  13. import androidx.annotation.RequiresApi;
  14. import androidx.core.app.ActivityCompat;
  15. import androidx.core.content.ContextCompat;
  16. import com.allqj.basic_lib.BasicLib;
  17. import java.util.Arrays;
  18. import java.util.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. public class PermissionUtil {
  22. public static int IGNORE_BATTERY_CODE = 1880;
  23. private static LinkedHashMap<String, List<String>> autoStartHashMap = new LinkedHashMap<String, List<String>>() {
  24. {
  25. put("XIAOMI", Arrays.asList(
  26. "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity",
  27. "com.miui.securitycenter/com.miui.permcenter.autostart.AutoStartManagementActivity",
  28. "com.miui.securitycenter"
  29. ));
  30. put("HUAWEI", Arrays.asList(
  31. "com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity",
  32. "com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity",
  33. "com.huawei.systemmanager/.optimize.process.ProtectActivity",
  34. "com.huawei.systemmanager/.optimize.bootstart.BootStartActivity",
  35. "com.huawei.systemmanager"
  36. ));
  37. // 荣耀
  38. put("HONOR", Arrays.asList(
  39. "com.hihonor.systemmanager/.appcontrol.activity.StartupAppControlActivity",
  40. "com.hihonor.systemmanager/.startupmgr.ui.StartupNormalAppListActivity",
  41. "com.hihonor.systemmanager"
  42. ));
  43. put("VIVO", Arrays.asList(
  44. "com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity",
  45. "com.vivo.permissionmanager/.activity.BgStartUpManagerActivity",
  46. "com.vivo.permissionmanager/.activity.PurviewTabActivity",
  47. "com.iqoo.secure/.ui.phoneoptimize.BgStartUpManager",
  48. "com.iqoo.secure/.safeguard.PurviewTabActivity",
  49. // "com.iqoo.secure/.ui.phoneoptimize.AddWhiteListActivity", //这是白名单, 不是自启动
  50. "com.vivo.permissionmanager",
  51. "com.iqoo.secure"
  52. ));
  53. put("OPPO", Arrays.asList(
  54. "com.android.settings/com.oplus.settings.feature.homepage.OplusSettingsHomepageActivity",
  55. "com.android.settings/com.oplus.settings.OplusSubSettings",
  56. "com.android.settings/com.android.settings.SubSettings",
  57. "com.oplus.safecenter/.startupapp.view.StartupAppListActivity",
  58. "com.coloros.safecenter/.startupapp.StartupAppListActivity",
  59. "com.coloros.safecenter/.permission.startup.StartupAppListActivity",
  60. "com.oppo.safe/.permission.startup.StartupAppListActivity",
  61. "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity",
  62. "com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity",
  63. "com.coloros.safecenter",
  64. "com.oppo.safe",
  65. "com.coloros.oppoguardelf"
  66. ));
  67. put("MEIZU", Arrays.asList(
  68. "com.meizu.safe/.permission.SmartBGActivity",
  69. "com.meizu.safe/.permission.PermissionMainActivity",
  70. "com.meizu.safe"
  71. ));
  72. put("SAMSUNG", Arrays.asList(
  73. "com.samsung.android.sm_cn/com.samsung.android.sm.ui.ram.AutoRunActivity",
  74. "com.samsung.android.sm_cn/com.samsung.android.sm.ui.appmanagement.AppManagementActivity",
  75. "com.samsung.android.sm_cn/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity",
  76. "com.samsung.android.sm_cn/.ui.ram.RamActivity",
  77. "com.samsung.android.sm_cn/.app.dashboard.SmartManagerDashBoardActivity",
  78. "com.samsung.android.sm/com.samsung.android.sm.ui.ram.AutoRunActivity",
  79. "com.samsung.android.sm/com.samsung.android.sm.ui.appmanagement.AppManagementActivity",
  80. "com.samsung.android.sm/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity",
  81. "com.samsung.android.sm/.ui.ram.RamActivity",
  82. "com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity",
  83. "com.samsung.android.lool/com.samsung.android.sm.ui.battery.BatteryActivity",
  84. "com.samsung.android.sm_cn",
  85. "com.samsung.android.sm"
  86. ));
  87. put("ONEPLUS", Arrays.asList(
  88. "com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity",
  89. "com.oneplus.security"
  90. ));
  91. put("LETV", Arrays.asList(
  92. "com.letv.android.letvsafe/.AutobootManageActivity",
  93. "com.letv.android.letvsafe/.BackgroundAppManageActivity",
  94. "com.letv.android.letvsafe"
  95. ));
  96. put("ZTE", Arrays.asList(
  97. "com.zte.heartyservice/.autorun.AppAutoRunManager",
  98. "com.zte.heartyservice"
  99. ));
  100. //金立
  101. put("F", Arrays.asList(
  102. "com.gionee.softmanager/.MainActivity",
  103. "com.gionee.softmanager"
  104. ));
  105. //以下为未确定(厂商名也不确定)
  106. put("SMARTISANOS", Arrays.asList(
  107. "com.smartisanos.security/.invokeHistory.InvokeHistoryActivity",
  108. "com.smartisanos.security"
  109. ));
  110. //360
  111. put("360", Arrays.asList(
  112. "com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity",
  113. "com.yulong.android.coolsafe"
  114. ));
  115. //360
  116. put("ULONG", Arrays.asList(
  117. "com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity",
  118. "com.yulong.android.coolsafe"
  119. ));
  120. //酷派
  121. put("COOLPAD"/*厂商名称不确定是否正确*/, Arrays.asList(
  122. "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain",
  123. "com.yulong.android.security"
  124. ));
  125. //联想
  126. put("LENOVO"/*厂商名称不确定是否正确*/, Arrays.asList(
  127. "com.lenovo.security/.purebackground.PureBackgroundActivity",
  128. "com.lenovo.security"
  129. ));
  130. put("HTC"/*厂商名称不确定是否正确*/, Arrays.asList(
  131. "com.htc.pitroad/.landingpage.activity.LandingPageActivity",
  132. "com.htc.pitroad"
  133. ));
  134. //华硕
  135. put("ASUS"/*厂商名称不确定是否正确*/, Arrays.asList(
  136. "com.asus.mobilemanager/.MainActivity",
  137. "com.asus.mobilemanager"
  138. ));
  139. //酷派
  140. put("YULONG", Arrays.asList(
  141. "com.yulong.android.softmanager/.SpeedupActivity",
  142. "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain",
  143. "com.yulong.android.security"
  144. ));
  145. }
  146. };
  147. private static LinkedHashMap<String, List<String>> bgPopHashMap = new LinkedHashMap<String, List<String>>() {
  148. {
  149. put("XIAOMI", Arrays.asList(
  150. "com.miui.securitycenter/com.miui.permcenter.permissions.PermissionsEditorActivity",
  151. "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity",
  152. "com.miui.securitycenter"
  153. ));
  154. put("HUAWEI", Arrays.asList(
  155. "com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity",
  156. "com.huawei.systemmanager"
  157. ));
  158. // 荣耀
  159. put("HONOR", Arrays.asList(
  160. "com.hihonor.systemmanager/.appcontrol.activity.StartupAppControlActivity",
  161. "com.hihonor.systemmanager/.startupmgr.ui.StartupNormalAppListActivity",
  162. "com.hihonor.systemmanager"
  163. ));
  164. put("VIVO", Arrays.asList(
  165. "com.vivo.permissionmanager/.activity.StartBgActivityControlActivity",//需要com.vivo.permission.manage.permission.ACCESS权限
  166. "com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity",
  167. "com.vivo.permissionmanager/.activity.PurviewTabActivity",
  168. "com.vivo.permissionmanager",
  169. "com.iqoo.secure"
  170. ));
  171. put("MEIZU", Arrays.asList(
  172. "com.meizu.safe/.permission.PermissionMainActivity",
  173. "com.meizu.safe"
  174. ));
  175. put("OPPO", Arrays.asList(
  176. "com.oplus.battery/com.oplus.powermanager.fuelgaue.PowerControlActivity",//not exported
  177. "com.android.settings/com.oplus.settings.OplusSubSettings",
  178. "com.android.settings/com.android.settings.SubSettings",
  179. "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity",
  180. "com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity",
  181. "com.coloros.safecenter",
  182. "com.oppo.safe",
  183. "com.coloros.oppoguardelf"
  184. ));
  185. put("SAMSUNG", Arrays.asList(
  186. "com.samsung.android.lool",
  187. "com.samsung.android.sm_cn",
  188. "com.samsung.android.sm"
  189. ));
  190. put("ONEPLUS", Arrays.asList(
  191. "com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity",
  192. "com.oneplus.security"
  193. ));
  194. put("LETV", Arrays.asList(
  195. "com.letv.android.letvsafe/.AutobootManageActivity",
  196. "com.letv.android.letvsafe/.BackgroundAppManageActivity",
  197. "com.letv.android.letvsafe"
  198. ));
  199. put("ZTE", Arrays.asList(
  200. "com.zte.heartyservice"
  201. ));
  202. //金立
  203. put("F", Arrays.asList(
  204. "com.gionee.softmanager/.MainActivity",
  205. "com.gionee.softmanager"
  206. ));
  207. //以下为未确定(厂商名也不确定)
  208. put("SMARTISANOS", Arrays.asList(
  209. "com.smartisanos.security/.invokeHistory.InvokeHistoryActivity",
  210. "com.smartisanos.security"
  211. ));
  212. //360
  213. put("360", Arrays.asList(
  214. "com.yulong.android.coolsafe"
  215. ));
  216. //360
  217. put("ULONG", Arrays.asList(
  218. "com.yulong.android.coolsafe"
  219. ));
  220. //酷派
  221. put("COOLPAD"/*厂商名称不确定是否正确*/, Arrays.asList(
  222. "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain",
  223. "com.yulong.android.security"
  224. ));
  225. //联想
  226. put("LENOVO"/*厂商名称不确定是否正确*/, Arrays.asList(
  227. "com.lenovo.security/.purebackground.PureBackgroundActivity",
  228. "com.lenovo.security"
  229. ));
  230. put("HTC"/*厂商名称不确定是否正确*/, Arrays.asList(
  231. "com.htc.pitroad/.landingpage.activity.LandingPageActivity",
  232. "com.htc.pitroad"
  233. ));
  234. //华硕
  235. put("ASUS"/*厂商名称不确定是否正确*/, Arrays.asList(
  236. "com.asus.mobilemanager/.MainActivity",
  237. "com.asus.mobilemanager"
  238. ));
  239. //酷派
  240. put("YULONG", Arrays.asList(
  241. "com.yulong.android.softmanager/.SpeedupActivity",
  242. "com.yulong.android.security/com.yulong.android.seccenter.tabbarmain",
  243. "com.yulong.android.security"
  244. ));
  245. }
  246. };
  247. private static LinkedHashMap<String, List<String>> powerSavingHashMap = new LinkedHashMap<String, List<String>>() {
  248. {
  249. put("XIAOMI", Arrays.asList(
  250. "com.miui.powerkeeper/.ui.HiddenAppsConfigActivity",
  251. "com.miui.securitycenter/com.miui.appmanager.ApplicationsDetailsActivity",
  252. "com.miui.powerkeeper",
  253. "com.miui.securitycenter"
  254. ));
  255. put("HUAWEI", Arrays.asList(
  256. "com.android.settings/.Settings$HighPowerApplicationsActivity",
  257. "com.android.settings"
  258. ));
  259. // 荣耀
  260. put("HONOR", Arrays.asList(
  261. "com.hihonor.systemmanager/.power.ui.HwPowerManagerActivity",
  262. "com.hihonor.systemmanager"
  263. ));
  264. put("VIVO", Arrays.asList(
  265. "com.vivo.abe/com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity",
  266. "com.iqoo.powersaving/.PowerSavingManagerActivity",
  267. "com.iqoo.powersaving"
  268. ));
  269. put("OPPO", Arrays.asList(
  270. "com.oplus.battery/com.oplus.powermanager.fuelgaue.PowerControlActivity",//not exported
  271. "com.android.settings/com.oplus.settings.OplusSubSettings",
  272. "com.android.settings/com.android.settings.SubSettings",
  273. "com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerConsumptionActivity",
  274. "com.coloros.oppoguardelf"
  275. ));
  276. }
  277. };
  278. public static void startToPermissionSetting(Context context, String type) {
  279. Log.d("Util", "******************当前手机型号为:" + Build.MANUFACTURER);
  280. LinkedHashMap<String, List<String>> permissionMap = new LinkedHashMap<>();
  281. if ("autoStart".equals(type)) {
  282. permissionMap = autoStartHashMap;
  283. } else if ("powerSaving".equals(type)) {
  284. permissionMap = powerSavingHashMap;
  285. } else if ("bgPop".equals(type)) {
  286. permissionMap = bgPopHashMap;
  287. }
  288. if (!permissionMap.keySet().contains(Build.MANUFACTURER.toUpperCase())){
  289. Intent intent = new Intent(Settings.ACTION_SETTINGS);
  290. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  291. context.startActivity(intent);
  292. return;
  293. }
  294. for (Map.Entry<String, List<String>> entry : permissionMap.entrySet()) {
  295. String manufacturer = entry.getKey();
  296. List<String> actCompatList = entry.getValue();
  297. if (Build.MANUFACTURER.equalsIgnoreCase(manufacturer)) {
  298. for (String act : actCompatList) {
  299. try {
  300. Intent intent;
  301. if (act.contains("/")) {
  302. Log.d("Util", "******************act为:" + act);
  303. intent = new Intent();
  304. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  305. ComponentName componentName = ComponentName.unflattenFromString(act);
  306. intent.setComponent(componentName);
  307. intent.putExtra("packagename", context.getPackageName());//vivo-SoftPermissionDetailActivity
  308. intent.putExtra("extra_pkgname", context.getPackageName());//xiaomi-PermissionsEditorActivity
  309. intent.putExtra("package_name", context.getPackageName());//xiaomi-ApplicationsDetailsActivity
  310. } else {
  311. //所以我是直接跳转到对应的安全管家/安全中心
  312. intent = context.getPackageManager().getLaunchIntentForPackage(act);
  313. }
  314. context.startActivity(intent);
  315. break;
  316. } catch (Exception e) {
  317. e.printStackTrace();
  318. Intent intent = new Intent(Settings.ACTION_SETTINGS);
  319. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  320. context.startActivity(intent);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. public static boolean isIgnoringBatteryOptimizations(Context context) {
  327. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  328. PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  329. if (powerManager != null) {
  330. return powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
  331. } else {
  332. return false;
  333. }
  334. } else {
  335. return true;
  336. }
  337. }
  338. @RequiresApi(api = Build.VERSION_CODES.M)
  339. public static void requestIgnoreBatteryOptimizations(Activity context) {
  340. try {
  341. Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
  342. intent.setData(Uri.parse("package:" + context.getPackageName()));
  343. context.startActivityForResult(intent, IGNORE_BATTERY_CODE);
  344. } catch (Exception e) {
  345. e.printStackTrace();
  346. }
  347. }
  348. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/247588
推荐阅读
相关标签
  

闽ICP备14008679号