警告:为了确保应用的安全性,启动 Service
时,请始终使用显式 Intent,且不要为服务声明 Intent 过滤器。使用隐式 Intent 启动服务存在安全隐患,因为您无法确定哪些服务将响应 Intent,且用户无法看到哪些服务已启动。从 Android 5.0(API 级别 21)开始,如果使用隐式 Intent 调用 bindService()
,系统会抛出异常
Caution: If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity()
. To first verify that an app exists to receive the intent, call resolveActivity()
on your Intent
object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity()
. If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.(调用隐士activity时,如果系统找不到,那么应用会崩溃,此时应先调用resolveActivity()
确保有activity会响应)