AndroidIntent简介

少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述AndroidIntent简介相关的知识,希望能为你提供帮助。

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



AndroidIntent简介

文章图片

 
AndroidIntent简介

文章图片
AndroidIntent简介

文章图片
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical"> 5 6< Button 7android:layout_width="wrap_content" 8android:layout_height="wrap_content" 9android:onClick="toMain" 10android:text="显示Intent启动自己" 11android:textAllCaps="false" /> 12 13< Button 14android:layout_width="wrap_content" 15android:layout_height="wrap_content" 16android:onClick="setComponent" 17android:text="启动别包下的activity" 18android:textAllCaps="false" /> 19 20< Button 21android:layout_width="wrap_content" 22android:layout_height="wrap_content" 23android:onClick="action" 24android:text="ActionViewActivity响应此action" 25android:textAllCaps="false" /> 26 27< Button 28android:layout_width="wrap_content" 29android:layout_height="wrap_content" 30android:onClick="web" 31android:text="浏览网页" 32android:textAllCaps="false" /> 33 34< Button 35android:layout_width="wrap_content" 36android:layout_height="wrap_content" 37android:onClick="call1" 38android:text="系统拨号界面" 39android:textAllCaps="false" /> 40 41< Button 42android:layout_width="wrap_content" 43android:layout_height="wrap_content" 44android:onClick="call2" 45android:text="拨号" 46android:textAllCaps="false" /> 47 48 < /LinearLayout>

activity_main.xml
AndroidIntent简介

文章图片
AndroidIntent简介

文章图片
1 public class MainActivity extends AppCompatActivity { 2 3@Override 4protected void onCreate(Bundle savedInstanceState) { 5super.onCreate(savedInstanceState); 6setContentView(R.layout.activity_main); 7} 8 9public void toMain(View v) { 10Intent intent = new Intent(this, MainActivity.class); 11startActivity(intent); 12} 13 14public void setComponent(View v) { 15//首先创建一个空参的Intent对象 16Intent intent = new Intent(); 17//Component(包名,完整的类名) 18ComponentName componentName = new ComponentName("com.example.lesson9_activitylaunchmode", "com.example.lesson9_activitylaunchmode.MainActivity"); 19//设置目标组件 20intent.setComponent(componentName); 21startActivity(intent); 22 23} 24 25public void action(View v) { 26Intent intent = new Intent(Intent.ACTION_VIEW); 27//这里可能找不到能够响应这个ACTION_VIEW的目标组件,会报ActivityNotFound异常。所以可以做try-catch 28startActivity(intent); 29//创建一个ActionViewActivity活动,并注册,指定能响应ACTION_VIEW 30} 31 32public void web(View v) { 33Intent intent = new Intent(Intent.ACTION_VIEW); 34intent.setData(Uri.parse("http://www.baidu.com")); 35startActivity(intent); 36} 37 38//系统拨号界面,报错 39public void call1(View v) { 40Intent intent = new Intent(Intent.ACTION_DIAL); 41intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity"); 42startActivity(intent); 43} 44 45 46//拨号界面 47public void call2(View v) { 48Uri uri = Uri.parse("tel:18822818871"); 49Intent intent = new Intent(Intent.ACTION_DIAL, uri); 50startActivity(intent); 51} 52 }

MainActivity.java
AndroidIntent简介

文章图片
AndroidIntent简介

文章图片
1 public class ActionViewActivity extends AppCompatActivity { 2@Override 3protected void onCreate(@Nullable Bundle savedInstanceState) { 4super.onCreate(savedInstanceState); 5TextView tv = new TextView(this); 6tv.setText("这是这是ActionViewActivity"); 7setContentView(tv); 8} 9 }

ActionViewActivity .java
AndroidIntent简介

文章图片
AndroidIntent简介

文章图片
1 < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" 2android: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 
 
 
【AndroidIntent简介】 

























    推荐阅读