复制步骤:
val url = "https://mylink.com"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val resolveInfo = context.packageManager.resolveActivity(intent, 0)
if (resolveInfo != null) {
startActivity(intent)
} else {
// error handling because no browser is installed
}
我的应用程序崩溃:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW dat=https://mylink.com.... cmp=com.mxtech.videoplayer.ad/com.mxtech.videoplayer.ActivityWebBrowser } from ProcessRecord{64f9702 20760:com.myapp/u0a65} (pid=20760, uid=10065) not exported from uid 10139
at android.os.Parcel.createException + 1966(Parcel.java:1966)
at android.os.Parcel.readException + 1934(Parcel.java:1934)
at android.os.Parcel.readException + 1884(Parcel.java:1884)
at android.app.IActivityManager$Stub$Proxy.startActivity + 3618(IActivityManager.java:3618)
at android.app.Instrumentation.execStartActivity + 1673(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult + 4689(Activity.java:4689)
at androidx.fragment.app.FragmentActivity.startActivityForResult + 767(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult + 4647(Activity.java:4647)
at androidx.fragment.app.FragmentActivity.startActivityForResult + 754(FragmentActivity.java:754)
at android.app.Activity.startActivity + 5008(Activity.java:5008)
at android.app.Activity.startActivity + 4976(Activity.java:4976)
at com.myapp.MyActivity.someFunction
仅当所有浏览器均已卸载/禁用时,我才看到此问题。在这种情况下,MX Player似乎会自己安装为浏览器,但无法处理从我的应用程序打开的链接。
答案 0 :(得分:3)
您可以检查“活动”以查看URL是否已解析并使用以下内容导出:
private fun openUrl(url: String?) {
if (url == null) return
val intent = Intent(Intent.ACTION_VIEW)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.data = Uri.parse(url)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
activity?.let {
val activityInfo = intent.resolveActivityInfo(it.packageManager, intent.flags)
if (activityInfo != null && activityInfo.exported) {
ActivityCompat.startActivity(it, intent, null)
}
}
}