Android知识总结(问答形式)一

本文详细解析了Android中的Activity与Broadcast Receiver的区别,包括它们的使用场景、IntentFilter的作用及配置方法,并探讨了Category Default的重要性以及Exported属性的影响。

Activity和receiver的区别

总感觉有点类似,但是:
Note: For all activities, you must declare your intent filters in the manifest file. However, filters for broadcast receivers can be registered dynamically by calling registerReceiver(). You can then unregister the receiver with unregisterReceiver(). Doing so allows your app to listen for specific broadcasts during only a specified period of time while your app is running.

在receiver中可以启动activity,但是不推荐。

broadcast出来的intent可以针对好多接受者,是个事件通知功能。
用intent 启动activity只能启动一个

category default必须定义?

To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file.

To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.
那,我不定义category default的话,explicit intent不需要intent filter,而implicit intent调用的话,又调用不到。岂不是没定义category default的intent filter没用?
但是,发现打开app的第一个activity的category中没有default category,只有launcher。

The CATEGORY_LAUNCHER category indicates that this activity’s icon should be placed in the system’s app launcher. If the element does not specify an icon with icon, then the system uses the icon from the element.

所以,这就理解了,主要是系统启动应用并不是用startActivity,所以没事。

exported? browsable? intentfilter?

exported:
如果不是exported的,用root可以调用起来。
如果没有指定exported,那么有intent filter则是exported,没有就不是。
就算不是exported,也可能通过一个exported的broadcast receiver调起来,但可能性不大?

browsable:
如果定义

    <intent-filter>
      ...
      <data android:scheme="https" android:host="www.example.com" />
      <data android:scheme="app" android:host="open.my.app" />
    </intent-filter>

那么It might seem as though this supports only https://www.example.com and app://open.my.app.
However, it actually supports those two, plus these: app://www.example.com and https://open.my.app.
所以,得分成两个intent filter写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值