IllegalArgumentException:Service
2020-07-15 本文已影响0人
phoebeyufish
昨天Android遇到这样一个异常,定位在这句话:
intent = new Intent("MusicService");
报错是:
java.lang.IllegalArgumentException:Service Intent must be explicit
在Activity中启动Service的时候报错: 服务意图必须是显性声明。这是为了防止造成冲突(i.e. 有多个Service用同样的intent-filter的情况)
这是Android5.0 (Lollipop) 之后的规定。
解决方案就是:改成用显示Intent,指定目标组件:
intent = new Intent(MainActivity.this, MusicService.class);