서비스 onStartCommand 방법 반환 START_STICKY에서 모든 시간
1)를 실행하는 서비스를 유지하기 위해 아래 단계에서 언급 한 바와 같이 당신은 서비스를 만들 수 있습니다.
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
2)는 관계없이 항상 경계 클라이언트의 수의 활성 상태를 유지하도록 startService (이면 MyService)를 사용하여 백그라운드에서 서비스를 시작합니다.
Intent intent = new Intent(this, PowerMeterService.class);
startService(intent);
3) 바인더를 만듭니다.
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
4) 서비스 연결을 정의하십시오.
private ServiceConnection m_serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
m_service = ((MyService.MyBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
m_service = null;
}
};
5) bindService를 사용하여 서비스에 바인딩합니다.
Intent intent = new Intent(this, MyService.class);
bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
끈끈한 서비스를 사용합니다. –