其实方法有很多中,在这里我介绍一种无意间看到的。有时候有很多需求,客户给你一个 apk,再网上下载一个 apk ,如何去启动它呢。在这里很多人就会说了,直接安装,点击应用图标啊,废话。如果可以这样操作的话,那还说个锤子。在现实的工作中,会遇到各种各样的奇葩场景,很多时候不会把所有的东西都给你准备好,如果真这样,那真没啥事了。废话不多说,直接介绍:
1.首先我们把 apk 安装到手机上,打开应用,查看 Locat 控制台,过滤掉 ActivityManager ,这个时候你就会发现很多东西了,直接是可以看到打开的 Activity 页面的全路径以及应用的包名,比如:
2020-04-18 17:05:37.336 1589-1604/? I/chatty: uid=1000(system) ActivityManager expire 2 lines
2020-04-18 17:05:40.711 1589-4854/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.tencent.mm/.ui.LauncherUI bnds=[335,749][503,917] (has extras)} from uid 10042
2020-04-18 17:05:40.779 1589-2595/? W/ActivityManager: Request to remove task ignored for non-existent task 5583
2020-04-18 17:05:41.726 1589-1614/? I/ActivityManager: Displayed com.tencent.mm/.ui.LauncherUI: +956ms
2020-04-18 17:05:42.424 1589-1606/? I/ActivityManager: Start proc 22884:com.tencent.mm:toolsmp/u0a164 for broadcast com.tencent.mm/.booter.MMReceivers$ToolsMpProcessReceiver caller=com.tencent.mm
2020-04-18 17:05:44.845 1589-1606/? I/ActivityManager: Start proc 22955:com.tencent.mm:sandbox/u0a164 for service com.tencent.mm/.service.ProcessService$SandBoxProcessService caller=com.tencent.mm
这是打开了微信应用,可以轻松看到微信的包名是:com.tencent.mm
启动页面是:.ui.LauncherUI,这样再配合代码打开应用。
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
就这样基本上是解决了我们在一些特殊场景下启动一个应用。