启动端
final Intent implicitIntent = new Intent();
final ComponentName component = new ComponentName(packageName,
className);
Intent explicitIntent = new Intent(implicitIntent);
explicitIntent.setAction(CMD_ACTION);
explicitIntent.putExtra(KEY_TYPE, keyType);
explicitIntent.putExtra(KEY_PARAMTER, parms);
explicitIntent.setComponent(component);
context.startService(explicitIntent);
接收端口
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
Logger.i(TAG, "onStartCommand::action : " + action);
if (ServiceConstant.CMD_ACTION.equals(action)) {
handleIntent(intent);
}
}
return START_STICKY;
}
private void handleIntent(Intent intent) {
int keyType = intent.getIntExtra(ServiceConstant.KEY_TYPE, -1);
if (keyType == VoiceConstant.OPEN_ECAR) {
ECarMgr.getInstance(this).initEcar();
}
String[] data = intent.getStringArrayExtra(ServiceConstant.KEY_PARAMTER);
}
启动service方法
最新推荐文章于 2021-05-26 07:14:42 发布