尽管已设置,intent.getCategories()为null

时间:2016-04-29 15:23:40

标签: android android-intent intentfilter

我使用此intent-filter配置让Activity在被网站中嵌入的脚本调用时启动我的应用程序:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="myapp" />
</intent-filter>

这很好用,因为我的测试设备HTC One M8和Android 6.0按照设计启动了应用程序。我可以使用活动onCreate方法中的此代码访问网址的查询参数:

Intent intent = getIntent();
  if (intent != null) {
    if (intent.getAction() != null) {
      if (intent.getAction().equals(Intent.ACTION_VIEW)) {
        if (intent.getCategories() != null) {
          if (intent.getCategories().contains(Intent.CATEGORY_BROWSABLE)) {
            Uri uri = intent.getData();
            uri.getQueryParameter("id")
            // launch another activity with this information
          }}}}} // flattened for this question

不幸的是,一个测试设备是带有Android 6.0的三星Galaxy S6。我无法通过查询参数,因为日志状态intent.getCategories()null。如何使用HTC但不能使用三星设备?

我的假设是Galaxy S6可能拥有比HTC更多的RAM,因此可能存储更长的活动(?),这导致Intent仍然是在schema意图之前运行应用程序的初始意图-filter开始了活动。任何想法如何确保应用程序收到它后立即使用schema意图?

1 个答案:

答案 0 :(得分:2)

Category test州的文档

  

对于通过类别测试的意图,Intent中的每个类别都必须与过滤器中的类别匹配。

这也意味着,当意图本身没有类别时,意图可能会通过此测试。因此,您不应该依赖这些类别,因此返回null。此外,不同的设备预先安装了不同的浏览器。而这些可能会产生不同的意图。三星可能不认为你的脚本链接可以浏览。

相反,您应该只依赖于操作和数据

Intent intent = getIntent();
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
    Uri data = intent.getData();
    if (data != null && "myapp".equals(data.getScheme())) {
        data.getQueryParameter("id")
        // launch another activity with this information
    }
}

或者甚至可能只根据你想要的数据。

相关问题