Android 打开外部应用(微博/微信/QQ等)

跳转微信首页

Intent intent = new Intent();
ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivityForResult(intent, 0);

通过以上代码就可跳转到微信首页。

错误处理

在跳转微信的时候,我们还必须要考虑一个问题,那就是:

用户可能在手机中没有安装微信

此时,当我们运行以上的代码,应用就会报class not found的错误。

解决方法:

在跳转模块外加一个 try…catch 模块,捕获异常,在catch模块中进行错误处理。

try {
        Intent intent = new Intent();
        ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(cmp);
        startActivityForResult(intent, 0);
    } catch (Exception e) {
        //若无法正常跳转,在此进行错误处理
        Toast.makeText(MainActivity.this, "无法跳转到微信,请检查您是否安装了微信!", Toast.LENGTH_SHORT).show();
    }

其他外部应用

跳转其他应用页面,和跳转微信首页类似。只需将 ComponentName 中的 Package Name(包名) 和 Class Name(类名)相应的替换掉就可以了。

ComponentName("Package Name","Class Name");

常用的Package命令:

ApplicationPackage NameClass Name
新浪微博(编辑界面)com.sina.weibocom.sina.weibo.EditActivity
腾讯微博(编辑界面)com.tencent.WBlogcom.tencent.WBlog.activity.MicroblogInput
微信com.tencent.mmcom.tencent.mm.ui.LauncherUI
QQcom.tencent.mobileqqcom.tencent.mobileqq.activity.HomeActivity

参考文章:

http://www.cnblogs.com/crane13/p/3715203

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值