应用程序在 android.content.ActivityNotFoundException 的某些设备上崩溃

App is crashing on some devices with android.content.ActivityNotFoundException

我的应用程序在某些设备上崩溃并出现错误 android.content.ActivityNotFoundException。在 Android Vitals Crashes 部分查找后,以下函数导致它崩溃:

private void openUrl(String url) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

我打算用 url 作为我的 Play 商店应用程序 link 打开我的应用程序的 Play 商店页面。 我无法在我的任何设备中重现崩溃。任何帮助将不胜感激。

不保证任何给定的 ACTION_VIEW Intent 都有效。例如,活跃用户可能拥有受限配置文件或属于某些工作配置文件的一部分,他们无法访问您尝试启动的任何内容。

考虑到 Android 11,最安全的解决方案是捕获异常:

private void openUrl(String url) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    try {
      startActivity(i);
    } catch (Exception e) {
      // do something to tell the user "sorry!"
    }
}