【问题标题】:ActivityNotFoundException when opening a URL in browser on notification click在通知单击时在浏览器中打开 URL 时出现 ActivityNotFoundException
【发布时间】:2020-04-08 22:19:28
【问题描述】:

我正在尝试在点击通知时打开一个 URL。安装了 2 个浏览器,包括 Chrome,但代码不起作用。

我正在使用这个:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieURL));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(browserIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, browserIntent, PendingIntent.FLAG_UPDATE_CURRENT);

通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher_foreground)
                        .setContentTitle(movieName + " is available")
                        .setContentText("Click here to open in browser")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                notificationManager.notify(1, builder.build());

错误: No Activity found to handle Intent { act=android.intent.action.VIEW dat=link>https: flg=0x10008000 }

Android 版本为 6,如果相关,我在 AsyncTask 中将其作为服务运行。 URL 没有任何问题。我之前从它那里得到了数据,并且没有丢失“https://”部分。

我是 android 新手,所以我希望得到一个简化的解释 :)

【问题讨论】:

  • 我想问题出在错误的 url
  • 只需尝试将方案添加到您的网址https://yts.lt。您的网址上没有方案,所以,我认为这是一个真正的问题
  • 只是好奇。你的手机上是否安装了浏览器
  • 他的意思是协议。而且您以错误的方式发布了网址。没有人能看到它一开始就有协议https://。不要使用引号而是打勾。 (好吧,正确的刻度,有几个)。
  • dat=link>https: -- 您的网址似乎无效。如果您尝试在 Web 浏览器的地址栏中粘贴 link>https:,您将无法获得网页。 “我将 url 打印到日志以进行检查”——Intent 似乎没有该 URL。它有link>https:

标签: android android-intent browser push-notification android-pendingintent


【解决方案1】:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieURL));

我不确定movieURL 包含什么,但是当你解析它时它会变成link>https:。这不是一个有效的 URL,Android 将找不到能够为其处理 ACTION_VIEW 的应用。

请注意,从您的应用中引发 NotificationNotification 在单击时会启动第三方应用)是不常见的。 Notification 背后的典型意义是允许用户控制应用程序行为的各个方面。如果您的目标是从您自己的应用程序中启动活动,请不要使用隐含的 Intent (new Intent(Intent.ACTION_VIEW, ...))。相反,请使用明确的Intent (new Intent(this, YourAwesomeActivity.class))。您仍然可以使用setAction()putExtra() 附加信息来告诉您的活动要显示什么。

【讨论】:

  • 这不在问题范围内,但它会同时打开浏览器显示通知。关于如何解决它的任何想法?
  • @TashilaPathum:确保​​你没有用那个Intent自己打电话给startActivity()
【解决方案2】:

请先检查一下,看看结果如何

if (browserIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(browserIntent);
} 

【讨论】:

  • 它只是隐藏了真正的问题,而不是解决它。最初的问题是关于为什么没有活动来解决简单的视图 URL 意图,但您的解决方案只是检查是否可以解决意图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 2020-05-05
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多