在android的应用程序能够有多个Activity,每一个Activity是同级别的,那么在启动程序时,最早启动哪一个Activity呢?有些程序可能须要显示在程序列表里,有些不须要。怎么定义呢?java
android.intent.action.MAIN 决定应用程序最早启动的Activity 。
android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里。android
由于你的程序可能有不少个activity,
只要xml配置文件中有这么一个intent-filter,并且里面有这个launcher,那么这个activity就是点击程序时最早运行的那个activity。app
<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>
intent必定要有的3个匹配值, action, category, data.
发送了这个intent以后,android会去系统里保存的MainManifest.xml清单(假设这个系统存放所有apk清单的文件为MainManifest.xml)里查找符合这两个属性的activity,而后启动它。ide
当某个Activity用startActivity(intentOther)方法向系统发送了一个intent(假如为 intentOther),那么android系统会去查找这个MainManifest.xml里注册的<intent-filter >属性,测试
查找到符合这个 intentOther 的就启动这个Activity,若是有多个这样的Activity符合条件的话,就跳出一个对话框让用户选择究竟要启动哪个this
<!-- display missed call --> <activity-alias android:name="@string/missedcallactivity" android:icon="@drawable/missing_icon" android:label="@string/missedcallactivity" android:targetActivity=".CustomLinphoneDialer" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
这个代码 能显示两个图标 ,咱们的目的是 能显示通话记录, 还得调用target activity, and show call history activity.
为了能支持隐式intent,activity、service和broadcast receiver会包含1到多个intent filter。每一个intent filter描述组件的可接收一组intent的能力。在intent filter中,说明了可接受的类型,以及不想要的intent。隐式的intent要想投递到一个组件,只需经过组件的一个filter便可。url
组件把filter分红多个,是为了针对具体不一样的任务。在sample中的Note pad示例中,NoteEditor activity有两个filter,一个用于启动并打开指定的note,另外一个是为了打开新的空的note。code
一个intent filter是一个IntentFilter类的实例。可是,android系统必须在组件未启动的状况下就知道它的能力,所以intent filter通常不会在java代码中设置,而是在应用的manifest文件中做为<intent-filter>元素的方式声明。一个例 外是,为broadcast receiver注册动态的filter,能够调用Context.registerReceiver()方法,经过直接实例化IntentFilter 对象建立。xml
filter有三个平等的部分:action、data和category。隐式intent将测试这三个部分。一个intent要想投递到一个组 件,那么这三个测试都要经过才行。固然若是组件有多个intent filter,可能一个intent没有经过,可是经过了另外的一个,这样也能够把intent投递到组件。对象
action测试在intent filter中能够包含多个action,好比:
<intent-filter . . . >
<action android:name="com.example.project.SHOW_CURRENT" />
<action android:name="com.example.project.SHOW_RECENT" />
<action android:name="com.example.project.SHOW_PENDING" />
. . .
</intent-filter>
要想经过测试,intent中的action名称要匹配其中之一。
若是intent filter中不包含action列表,而intent指定action,那么intent没有匹配的action,不经过;intent未指定action,而intent filter指定,会自动经过测试。
category测试在intent filter中可包含category列表:
<intent-filter . . . >
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
. . .
</intent-filter>
intent想经过测试,必须匹配一个intent filter中的category。
原理上讲,intent若是没有category设置,那么老是能够经过测试。这基本上是正确的,可是有一个例外。Android在为全部隐式 intent执行startActivity()方法的时候,会认为它们至少包含了一个 android.intent.category.DEFAULT。所以,若是activity想收到隐式intent,必须加入这个category。
date测试data元素在intent filter元素中,能够重复屡次(action和category不容许重复的),也能够根本没有。好比:
<intent-filter . . . >
<data android:mimeType="video/mpeg" android:scheme="http" . . . />
<data android:mimeType="audio/mpeg" android:scheme="http" . . . />
. . .
</intent-filter>
在data元素中指定uri和数据类型(MIME类型)。uri是被分开表示的:
scheme://host:port/path
其中host和port是关联的,若是host没有设置,port也会忽略。
全部这些属性都是可选的,可是不是独立的。好比,若是要设置path,那么也必须设置schema、host和port。
在比较intent中的uri和intent filter中指定的uri时,只会比较intent filter中说起的URL部分。好比,intent filter中只说起了schema,那么全部url包含这个schema的都匹配。在filter的path部分可使用通配符作到灵活的匹配。
mimeType属性,比uri方式更经常使用。intent和intent filter均可以使用mime通配符的方式,好比,text/*。
若是既有mimeType,又有uri的状况,比较规则以下:
<activity-alias 72 + android:description="@string/history" 73 + android:icon="@drawable/ic_launcher_history" 74 + android:label="@string/history" 75 + android:name="CustomHistoryActivity" 76 + android:targetActivity=".CustomLinphoneDialer" > 77 + <intent-filter > 78 + <action android:name="android.intent.action.MAIN" /> 79 + 80 + <category android:name="android.intent.category.LAUNCHER" /> 81 + </intent-filter> 82 + 83 + <meta-data 84 + android:name="dltype" 85 + android:value="@string/dialer_history" > 86 + </meta-data> 87 + </activity-alias>
程序中 判断处理
private void getlauncherFlag() { 819 » » // get launcher flag 820 » » try { 821 » » » ActivityInfo info = this.getPackageManager().getActivityInfo( 822 » » » » » getComponentName(), PackageManager.GET_META_DATA); 823 » » » launcherType = info.metaData.getString("dltype"); 824 » » } catch (Exception e) { 825 » » » e.printStackTrace(); 827 » » } 826 » » } 828 » } 827 » }
根据flag作跳转
private void applistLoginTransferView() { 1537 » » try { 1538 » » » Log.d(TAG, "launcherType=" + launcherType); 1539 » » » if (null != launcherType) { 1540 » » » » if (launcherType.equals(STRING_DIALER_CONTACT)) { 1541 » » » » » transfer2ContactList(); 1542 » » » » } else { 1543 » » » » » if (launcherType.equals(STRING_DIALER_HISTORY)) { 1544 » » » » » » transfer2HistoryList(); 1545 » » » » » } else { 1546 » » » » » » // do nothing. 1547 » » » » » } 1548 » » » » } 1549 » » » } 1550 » » } catch (Exception e) { 1551 » » » e.printStackTrace(); 1552 » » } 1521 » } 1553 » }
transfer2HistoryList();这个transfer2HistoryList是原来的跳起色制,略过。 到此activity-alias实现app另外程序入口并显示指定view完成。