Android Intent简介

Intent对象主要用来在Android程序的Activity,Service和BroadcastReceiver这3大组件之间传输数据,

而针对这3大组件,有独立的Intent传输机制,分别如下:
1、Activity:通过将一个Intent对象传递给Context.startActivity()或Activity.startActivityForResult(),
启动一个活动或者使用一个已经存在的活动去做新的事情。
2、Service:通过将一个Intent对象传递给Content.startService(),初始化一个Service或者传递一个新的指令给正在运行的Service;
类似的,通过将一个Intent对象传递给ContentBindService(),可以建立调用组件和目标服务之间的连接
3、BroadcastReceiver:通过将一个Intent对象传递给任何广播方法
(如:Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast()等,可以传递到所有感兴趣的广播接收者)

注意:在每种传输机制下,Android程序会自动查找合适的Activity,Service或者BroadcastReceiver来响应Intent(意图),
如果有必要的话,初始化他们,这些消息系统之间没有重叠,即广播意图只会传递给广播接收者,而不会传递给活动或者服务,反之亦然。


Intent通过下面的属性来描述以上的某个意图:
action:用来表示意图的动作,如:查看、发邮件、打电话
category:用来表示动作的类别
data:用来表示与动作要操作的数据。如:查看 联系人
type:对data类型的描述
extras:附加信息,如:详细资料,一个文件,某事
component:目标组件(显示Intent)

显示Intent
指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context,Class)来指定)。
通过指定具体的组件类,通知应用启动对应的组件

隐式Intent
没有指定Component属性的Intent.这些Intent需要包含足够的信息,这样系统才能够根据这些信息,在所有的可用组件中,确定满足此Intent的组件。

对于显示的Intent,Android不需要去解析,因为目标组件已经很明确,
Android需要解析的是那些隐式的Intent,通过解析,将Intent映射给可以处理此Intent的Activity、IntentReceiver或者Service

Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有Intent-filter及其定义的Intent,最终找到匹配的Intent。
1、如果Intent指明了action,则目标组件的Intent-Filter的action列表中就必须包含有这个action,否则不能匹配。
2、如果Intent没有提供type,系统将从data中得到数据类型,和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
3、如果Intent的数据不是content:类型的URI,而且Intent也没有明确指定它的type,将根据Intent中的数据的scheme(比如http;或者mailto:)进行匹配。
同上,Intent的scheme必须出现在目标组件的scheme列表中。
4、如果Intent指定了一个或多个category,这些类别必须全部出现在组件的类别列表中。比如Intent中包含两个类别:LAUNCH_CATEGORY和ALTERNATIVE_CATEORY,
解析得到的目标组件必须至少包含这两个类别。

只有<action>和<category>中的内容同时能够匹配上Intent中指定的action和category时,这个活动才能够响应该Intent


 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <Button
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:onClick="toMain"
10         android:text="显示Intent   启动自己"
11         android:textAllCaps="false" />
12 
13     <Button
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:onClick="setComponent"
17         android:text="启动别包下的activity"
18         android:textAllCaps="false" />
19 
20     <Button
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:onClick="action"
24         android:text="ActionViewActivity响应此action"
25         android:textAllCaps="false" />
26 
27     <Button
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:onClick="web"
31         android:text="浏览网页"
32         android:textAllCaps="false" />
33 
34     <Button
35         android:layout_width="wrap_content"
36         android:layout_height="wrap_content"
37         android:onClick="call1"
38         android:text="系统拨号界面"
39         android:textAllCaps="false" />
40 
41     <Button
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:onClick="call2"
45         android:text="拨号"
46         android:textAllCaps="false" />
47 
48 </LinearLayout>
activity_main.xml
 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7     }
 8 
 9     public void toMain(View v) {
10         Intent intent = new Intent(this, MainActivity.class);
11         startActivity(intent);
12     }
13 
14     public void setComponent(View v) {
15         //首先创建一个空参的Intent对象
16         Intent intent = new Intent();
17         //Component(包名,完整的类名)
18         ComponentName componentName = new ComponentName("com.example.lesson9_activitylaunchmode", "com.example.lesson9_activitylaunchmode.MainActivity");
19         //设置目标组件
20         intent.setComponent(componentName);
21         startActivity(intent);
22 
23     }
24 
25     public void action(View v) {
26         Intent intent = new Intent(Intent.ACTION_VIEW);
27         //这里可能找不到能够响应这个ACTION_VIEW的目标组件,会报ActivityNotFound异常。所以可以做try-catch
28         startActivity(intent);
29         //创建一个ActionViewActivity活动,并注册,指定能响应ACTION_VIEW
30     }
31 
32     public void web(View v) {
33         Intent intent = new Intent(Intent.ACTION_VIEW);
34         intent.setData(Uri.parse("http://www.baidu.com"));
35         startActivity(intent);
36     }
37 
38     //系统拨号界面,报错
39     public void call1(View v) {
40         Intent intent = new Intent(Intent.ACTION_DIAL);
41         intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
42         startActivity(intent);
43     }
44 
45 
46     //拨号界面
47     public void call2(View v) {
48         Uri uri = Uri.parse("tel:18822818871");
49         Intent intent = new Intent(Intent.ACTION_DIAL, uri);
50         startActivity(intent);
51     }
52 }
MainActivity.java
1 public class ActionViewActivity extends AppCompatActivity {
2     @Override
3     protected void onCreate(@Nullable Bundle savedInstanceState) {
4         super.onCreate(savedInstanceState);
5         TextView tv = new TextView(this);
6         tv.setText("这是这是ActionViewActivity");
7         setContentView(tv);
8     }
9 }
ActionViewActivity .java
 1 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
 2         android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
 3         <activity android:name=".MainActivity">
 4             <intent-filter>
 5                 <action android:name="android.intent.action.MAIN" />
 6 
 7                 <category android:name="android.intent.category.LAUNCHER" />
 8             </intent-filter>
 9         </activity>
10         <activity android:name=".ActionViewActivity">
11             <intent-filter>
12                 <action android:name="android.intent.action.VIEW"/>
13                 <category android:name="android.intent.category.DEFAULT"/>
14             </intent-filter>
15 
16         </activity>
17     </application>
AndroidManifest.xml

 

 

 

 
























posted on 2016-10-25 17:32  语风6649  阅读(259)  评论(0编辑  收藏  举报

导航