- 5.0之前支持的隐式启动服务方式
// action名称:com.wong.game.GAME_MyService
Intent intent = new Intent("com.wong.game.GAME_MyService");
context.startService(intent);
- 5.0之后不支持上述的方式,可以使用下面的式
// Service能够匹配的Action
Intent intent = new Intent("com.wong.game.GAME_MyService");
// 应用的包名
intent.setPackage("com.wong.game");
context.startService(intent);
- 最保险的做法就是使用显式启动服务方式
Intent intent = new Intent();
intent.setClass(mActivity,MyService.class);
// mActivity是当前的activity,MyService.class是要启动的服务
mActivity.startService(intent);
谢谢阅读