美文网首页
No Activity found to handle Inte

No Activity found to handle Inte

作者: 静水红阳 | 来源:发表于2019-11-22 11:44 被阅读0次

    问题说明

    项目内测试伙伴在测试Deeplink跳转时发现了一个闪退,查询错误日志得到原因是No Activity found to handle Intent

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=scheme://host?source=mainPage }
    

    查询具体的错误发生位置为调起Deeplink的代码,代码如下:

    var url = getDeepLink() ?: ""
    var intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    if (context != null ){
         tryStartActivity(context!!, intent)
    }
    

    原因分析

    此处代码是通过Intent启动方式调起第三方应用,但是没有完全考虑第三方应用没有存在的情况。
    如果Deeplink对应的第三方应用不存在的情况下,就有可能出现无法调起的错误,还有可能出现崩溃。

    解决办法

    发生错误的根本原因在于没有对应的Activity去承接Intent的处理,因此可以在在StartActivity之前对Intent进行判断,方法为intent.resolveActivity,并对不存在情况进行处理,示例代码如下:

    var url = getDeepLink() ?: ""
    var intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    if (context != null && intent.resolveActivity(context!!.packageManager)!=null) {
         tryStartActivity(context!!, intent)
    }
    

    相关文章

      网友评论

          本文标题:No Activity found to handle Inte

          本文链接:https://www.haomeiwen.com/subject/jfifwctx.html