Android APK启动Service的错误处理

作者: 网路元素 | 来源:发表于2017-09-10 11:18 被阅读16次

在Android4.2时,如下语句:

Intent intent = new Intent();

intent.setAction("com.app.media.music_service");

intent.putExtra("idOption", myPosition);

startService(intent);

能正常运行,但在Android5.1上,会有如下提示:

java.lang.IllegalArgumentException: Service Intent must be explicit

原来需要显示调用,故将代码修改为:

Intent intent = new Intent(MainActivity.this,MusicService.class);

intent.setAction("com.app.media.music_service");

intent.putExtra("idOption", myPosition);

startService(intent);

这样就能调用成功了。

相关文章

网友评论

    本文标题:Android APK启动Service的错误处理

    本文链接:https://www.haomeiwen.com/subject/nvmzjxtx.html