1、要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent。
Explicit Intent明确的指定了要启动的Acitivity ,比如以下Java代码:
Intent intent= new Intent(this, B.class)
Implicit Intent没有明确的指定要启动哪个Activity ,而是通过设置一些Intent Filter来让系统去筛选合适的Acitivity去启动。
2、intent到底发给哪个activity,需要进行三个匹配,一个是action,一个是category,一个是data。
理论上来说,如果intent不指定category,那么无论intent
filter的内容是什么都应该是匹配的。但是,如果是implicit
intent,android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent
filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。所以,如果你的
activity支持接收implicit intent的话就一定要在intent
filter中加入android.intent.category.DEFAULT。
例外情况是:android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题。
我们定义的activity如果接受implicit intent的话,intent filer就一定要加上android.intent.category.DEFAULT这个category。
来自另一篇文章的解释:
在写 AndroidManifest.xml 的时候,一直没有搞明白,什么时候要给 Activityandroid.intent.category.DEFAULT 过滤器,现在才明白。
--------------------------------------------------------------------------------
Android treats all implicit intents passed to startActivity() as if they
contained at least one category: "android.intent.category.DEFAULT" (the
CATEGORY_DEFAULT constant). Therefore, activities that are willing to
receive implicit intents must include "android.intent.category.DEFAULT"
in their intent filters
--------------------------------------------------------------------------------
意思是说,每一个通过 startActivity() 方法发出的隐式 Intent 都至少有一个 category,就是
"android.intent.category.DEFAULT",所以只要是想接收一个隐式 Intent 的 Activity 都应该包括
"android.intent.category.DEFAULT" category,不然将导致 Intent 匹配失败。
从上面的论述还可以获得以下信息:
1、一个 Intent 可以有多个 category,但至少会有一个,也是默认的一个 category。
2、只有 Intent 的所有 category 都匹配上,Activity 才会接收这个 Intent。
分享到:
相关推荐
恢复已经停止的更新下载。 'android.server.checkin.FOTA_RESTART' 通过 OTA 下载并安装操作系统更新。 'android.server.checkin.FOTA_UPDATE' 用户按下了'Media Button'。...'android.intent.action.PACKAGE_ADDED'
接收系统的时间流逝的广播,屏幕开启关闭的广播,电量改变的广播。这些广播在清单文件里注册时,看不到效果。在这个例子中通过在一个服务里面用代码注册的方式,成功接收了这些广播。
android各组件详解- Intent.
Android 屏幕唤醒 + 屏幕解锁 + 开机服务监听(android.intent.action.BOOT_COMPLET)添加权限:{//开机监听<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><uses-permission android...
监听自己被卸载通过前辈们探索发现,只能通过监听系统卸载log如:act=android.intent.action.DELETE dat=package:com.xyz.remove cmp=com.android.packageinstaller/.UninstallerActivity 通过 “android.intent....
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.VOICE_LAUNCH" /> <category android:name=...
安卓Android源码——Intent.rar
android Action call 拨打电话 Intent.ACTION.CALL
Intent intent = new Intent("android.intent.action.SILENT_PACKAGE_INSTALL"); intent.putExtra("silent_install_file", "/sdcard/Helloworld.apk"); intent.putExtra("silent_install_auto_start", true);//...
returnIt.setType("vnd.android-dir/mms-sms"); 11,直接发邮件 Uri smsToUri = Uri.parse("smsto://100861"); returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); returnIt.putExtra("sms_body", "shenrenkui...
自学 Android 做的第二十六个 Android 程序,使用了 Intent 和 Broadcast Receiver
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > android:key="settings" android:title="@string/settings" > <intent android:targetClass=...
intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/myFile/"+name )); ...
BATTERY_CHANGED_ACTION 充电状态,或者电池的电量发生变化 "android.intent.action.BATTERY_CHANGED" BOOT_COMPLETED_ACTION 在系统启动后,这个动作被广播一次(只有一次) "android.intent.action.BOOT_COMPLETED" ...
Android 广播大全 Intent Action 事件.
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程...
android:name="android.intent.category.LAUNCHER" /> </intent-filter> <activity android:name="ReviewList" android:label="@string/app_name_reviews"> <intent-filter> <category ...
主要为大家详细介绍了Android利用Intent启动和关闭Activity的相关操作,感兴趣的小伙伴们可以参考一下
Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build(); Intent intent = new Intent("android.intent.action.VIEW"); ...