最近实习要做一个需求的一部分是打开UC浏览器.
当然并不是那种使用UC浏览器打开一个网址,而是就像从launcher点开一样的打开.
到百度上搜,都说需要包名和主Activity名.实现起来也特别麻烦.自己尝试了半天也没有正确实现,结果在浏览PackageManager的方法的时候无意中发现一个叫getLaunchIntentForPackage的方法,只需要传入包名就能打开相应的应用,当然前提是你安装了,尝试了一下,成功了.
比如说我要打开UC浏览器,只需要知道UC浏览器的包名com.UCMobile,用下面的代码就可以调用
startActivity(getPackageManager().getLaunchIntentForPackage("com.UCMobile"));
但是我比较好奇网上的实现方法到底对不对,看了一下这个方法的源码,如下
@Override
public Intent getLaunchIntentForPackage(String packageName) {
// First see if the package has an INFO activity; the existence of
// such an activity is implied to be the desired front-door for the
// overall package (such as if it has multiple launcher entries).
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCatego