显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件。
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。
【2】对于隐式意图,Android是怎样寻找到这个最合适的组件呢?
1.动作测试
2.数据测试
a. 一个既不包含URI也不包含数据类型的意图对象仅在过滤器也同样没有指定任何URIs和数据类型的情况下才能通过测试。
c. 一个包含数据类型但不包含URI的意图对象仅在这个过滤器列举了同样的数据类型而且也没有指定一个URI的情况下才能通过测试。
3.类别测试
对于一个能够通过类别匹配测试的意图,意图对象中的类别必须匹配过滤器中的类别。这个过滤器可以列举另外的类别,但它不能遗漏在这个意图中的任何类别。
- <span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;"><strong>public class MainActivity extends Activity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button) this.findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- Intent intent = new Intent();
- intent.setAction("com.hoo.kay");
- //设置数据URI与数据类型匹配
- //intent.setData(data);
- //intent.setType(type);注意这个方法会清除setData的内容,所以如果既要设置类型与数据,那么使用setDataAndType
- intent.setDataAndType(Uri.parse("hoo://www.hoo.com/person"), "image/gif");
- startActivity(intent);
- }
- });
- }
- }</strong></span></span>
注意上面隐式意图设置匹配规则的方法,intent.setType(type);注意这个方法会清除setData的内容,所以如果既要设置类型与数据,那么使用setDataAndType
- <span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;"><strong><application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".OtherActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="com.hoo.kay" />
- <action android:name="com.hoo.kesen" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="com.hoo.category.li" />
- <data android:scheme="hoo" android:host="www.hoo.com" android:path="/person"/>
- <data android:mimeType="image/gif"/>
- </intent-filter>
- </activity>
- </application></strong></span></span>
以上转载:http://blog.csdn.net/dlutbrucezhang/article/details/8917774