最近运行了一下我以前做的一个启动service的demo程序,发现!不!好!用!了!看了一下Log异常:service intent must be explicit.什么鬼?什么鬼?什么鬼!上网查了一下,原来从Android 5.0 开始,google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛这个个异常出来.
if (service.getComponent() == null && service.getPackage() == null) {
if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
IllegalArgumentException ex = new IllegalArgumentException(
"Service Intent must be explicit: " + service);
throw ex;
} else {
Log.w(TAG, "Implicit intents with startService are not safe: " + service
+ " " + Debug.getCallers(2, 3));
}
}
}
也就是说在5.0里面,如果targetSdkVersion>=LOLLIPOP 就会报异常:“service intent must be explicit”,这就说明1.targetSdkVersion>=LOLLIPOP 2.手机系统为5.0以上版本 3.程序采用隐式Intent调用Service,这三个条件是问题发生的充分必要条件。