之前聊的关于启动Activtiy的都是在Intent对象中指明要启动哪一个Activtiy(Intent对象的构造中传参数或者Intent对象的setClass方法),这时一种显式的启动方式。其实还有一种隐式的启动方式,它不指明要开启的Activity类,而是通过匹配一些数据信息来寻找Activity。打个踢球叫人守门的比方,显式启动方式就是“xxx,你快去守门!”,隐式启动就是类似“谁以前守过门去守门”。要知道隐式启动Activity的原理,首先要理解Intent。
之前已经聊过Intent了,现在再深入一点,按照惯例来段开发文档介绍(从上次聊的文档部分后开始):
It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:
action -- The general action to be performed, such as ACTION_VIEW
, ACTION_EDIT
, ACTION_MAIN
, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri
.
大概意思就是Intent是一种携带对一种行为的抽象描述的数据的结构,在一个Intent对象中主要携带的信息有action和data,action指明将要进行的动作行为,data指明行为使用到的数据,而要隐式启动一个Activity,就是主要靠它们去匹配。
常用的action和data的配对如下:
ACTION_VIEW
content://contacts/people/1 -- Display information about the person whose identifier is "1".
ACTION_DIAL
content://contacts/people/1 -- Display the phone dialer with the person filled in.
ACTION_VIEW
tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.
ACTION_DIAL
tel:123 -- Display the phone dialer with the given number filled in.
ACTION_EDIT
content://contacts/people/1 -- Edit information about the person whose identifier is "1".
ACTION_VIEW
content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical top-level entry into the Contacts application, showing you the list of people. Selecting a particular person to view would result in a new intent { ACTION_VIEW
content://contacts/N } being used to start an activity to display that person.
category -- Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER
means it should appear in the Launcher as a top-level application, whileCATEGORY_ALTERNATIVE
means it should be included in a list of alternative actions the user can perform on a piece of data.
type -- Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.
component -- Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
extras -- This is a Bundle
of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
PackageManager
for a component that can handle the intent. The appropriate component is determined based on the intent information supplied in the AndroidManifest.xml
file.
android.intent.action.MAIN
"/>
android.intent.category.LAUNCHER
"/>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ // <action android:name="android.intent.action.VIEW" /> // <category android:name="android.intent.category.DEFAULT" /> // <category android:name="android.intent.category.BROWSABLE" /> // <data android:scheme="http" /> // <data android:scheme="https" /> // <data android:scheme="about" /> // <data android:scheme="javascript" /> Intent intent = new Intent(); //关键代码,分别为Intent对象定义了要访问Activtiy的action,category,data intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addCategory("android.intent.category.BROWSABLE"); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); } }
<span style="color:#333333;"> <activity android:name="BrowserActivity" android:label="@string/application_name" android:launchMode="singleTask" android:alwaysRetainTaskState="true" android:configChanges="orientation|keyboardHidden" android:theme="@style/BrowserTheme" android:windowSoftInputMode="adjustResize" > <intent-filter> <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- For these schemes were not particular MIME type has been supplied, we are a good candidate. --> </span><span style="color:#ff0000;"> <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="http" /> <data android:scheme="https" /> <data android:scheme="about" /> <data android:scheme="javascript" /> </intent-filter></span><span style="color:#333333;"> <!-- For these schemes where any of these particular MIME types have been supplied, we are a good candidate. --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> <data android:scheme="inline" /> <data android:mimeType="text/html"/> <data android:mimeType="text/plain"/> <data android:mimeType="application/xhtml+xml"/> <data android:mimeType="application/vnd.wap.xhtml+xml"/> </intent-filter> <!-- We are also the main entry point of the browser. --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> <!-- The maps app is a much better experience, so it's not worth having this at all... especially for a demo! <intent-filter android:label="Map In Browser"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/postal-address" /> </intent-filter> --> <intent-filter> <action android:name="android.intent.action.WEB_SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MEDIA_SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity></span>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { // 打 action // 人 数据 // 附件的数据 Category 类别 Intent intent = new Intent(); intent.setAction("com.itheima.intent2.open2"); intent.addCategory(Intent.CATEGORY_DEFAULT); // URL:统一资源定位符 http https ftp rtsp: URI:统一资源标识符 url是uri的一个子集 // intent.setData(Uri.parse("jianren:张三")); // intent.setType("application/person"); intent.setDataAndType(Uri.parse("jianren:张三"), "application/person"); startActivity(intent); } }
<activity android:name="com.itheima.intent2.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="com.itheima.intent2.SecondActivity" > <intent-filter> <action android:name="com.itheima.intent2.open2" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/person" /> <data android:scheme="jianren" /> </intent-filter> </activity>
ACTION_MAIN
ACTION_VIEW
ACTION_ATTACH_DATA
ACTION_EDIT
ACTION_PICK
ACTION_CHOOSER
ACTION_GET_CONTENT
ACTION_DIAL
ACTION_CALL
ACTION_SEND
ACTION_SENDTO
ACTION_ANSWER
ACTION_INSERT
ACTION_DELETE
ACTION_RUN
ACTION_SYNC
ACTION_PICK_ACTIVITY
ACTION_SEARCH
ACTION_WEB_SEARCH
ACTION_FACTORY_TEST
ACTION_MAIN
和ACTION_EDIT最为常见。
常见的Category:
CATEGORY_DEFAULT
CATEGORY_BROWSABLE
CATEGORY_TAB
CATEGORY_ALTERNATIVE
CATEGORY_SELECTED_ALTERNATIVE
CATEGORY_LAUNCHER
CATEGORY_INFO
CATEGORY_HOME
CATEGORY_PREFERENCE
CATEGORY_TEST
CATEGORY_CAR_DOCK
CATEGORY_DESK_DOCK
CATEGORY_CAR_MODE
BroadcastReceiver
和Service都将运用到它,以后再聊。