无法理解意图过滤器

5

我正在阅读Intent和Intent Filter,我找到了下面的代码:

在Activity中:

 Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));  
 startActivity(i);

在清单文件中:

<activity android:name="com.example.intentdemo.CustomActivity" 
        android:label="@string/app_name">            
  <intent-filter>               
   <action android:name="android.intent.action.VIEW" /> 
   <action android:name="com.example.intentdemo.LAUNCH" /> 
   <category android:name="android.intent.category.DEFAULT" />
   <data android:scheme="http" />            
  </intent-filter>         
</activity>

我的问题是,我是否应该在意图中声明android.content.Intent.ACTION_VIEW而不是android.Intent.ACTION_VIEW

我正在为你的HTTP问题撰写答案,为什么你把它删除了? - user3301551
3个回答

1

android.content.Intent.ACTION_VIEW 是指类 android.content.Intent 中常量 ACTION_VIEW 的名称。该常量的值为 "android.intent.action.VIEW"。因此有所区别。


我们可以补充一下,对于将Android系统的第一个意图类命名为这样的方式确实有点令人困惑。 - Snicolas

0
如果您查看Intent类的源代码,ACTION_VIEW是一个String常量,其值为"android.intent.action.VIEW"...
public static final String ACTION_VIEW = "android.intent.action.VIEW";

所以两者都指向相同的值,即"android.intent.action.VIEW"...


0

你可能会混淆 android.intent.action.VIEWandroid.Intent.ACTION_VIEW。它们是完全不同的。

你正在使用的是隐式意图

这些意图不指定目标,组件名称字段留空。隐式意图通常用于激活其他应用程序中的组件。

Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));  
 startActivity(i);

操作

Intent 对象是一个命名要执行的操作或广播意图中报告的已发生的操作的字符串。操作在很大程度上决定了意图对象的其余结构。Intent 类定义了多个与不同意图对应的操作常量。请查看Android Intent标准操作列表。

Intent 对象中的操作可以通过 setAction() 方法进行设置,并且可以通过 getAction() 进行读取。

查看更多关于 Intent 和 Intent-Filter 的信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接