0
点赞
收藏
分享

微信扫一扫

Android5.0之后不支持隐式启动服务必须是显式的


  1. 5.0之前支持的隐式启动服务方式

// action名称:com.wong.game.GAME_MyService
Intent intent = new Intent("com.wong.game.GAME_MyService");
context.startService(intent);

  1. 5.0之后不支持上述的方式,可以使用下面的式

// Service能够匹配的Action
Intent intent = new Intent("com.wong.game.GAME_MyService");
// 应用的包名
intent.setPackage("com.wong.game");
context.startService(intent);

  1. 最保险的做法就是使用显式启动服务方式

Intent intent = new Intent();
intent.setClass(mActivity,MyService.class);
// mActivity是当前的activity,MyService.class是要启动的服务
mActivity.startService(intent);

谢谢阅读


举报

相关推荐

0 条评论