start
메서드가있는 프레임 워크를 캡슐화하는 Android 서비스가 있습니다. 이 서비스는이 같은 것으로 귀결, 많은 것들이 생략 :응용 프로그램 시작시 서비스 바인드 및 시작
public class MyServiceBridge implements ServiceConnection {
private boolean started = false;
private boolean bound = false;
private MyService myService;
public MyServiceBridge(Context context){
this.context = context;
}
public void bindService(){
Intent intent = new Intent(getContext(), MyService.class);
getContext().bindService(intent, this, getContext().BIND_AUTO_CREATE);
getContext().startService(intent);
}
// Here's a sample call, and the one that is relevant
public void start(Map<String, Object> options){
setOptions(options);
if(bound == true){
getMyService().start(options);
}
else{
started = true;
}
}
}
내가 실행하기 위해 다리의 start
메소드를 호출
public class MyService extends Service {
private IBinder thisBinder;
public MyService(){
thisBinder = new LocalBinder();
}
@Override
public IBinder onBind(Intent intent) {
return thisBinder;
}
public void start(Map<String, Object> options)
{
getDriverManager().start(options);
}
}
내가 또한 서비스 호출을 브리징 클래스가 서비스. 이것은이 특별한 상황을 제외하고는 잘 작동합니다 (지금까지). MyApplication
클래스는 onCreate
에 다리의 시작 메서드를 호출
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
getServiceBridge().start(null);
}
}
이것은, according to the docs은 "응용 프로그램이 시작할 때 모든 활동, 서비스, 또는 수신기 객체 전에 생성 된 (콘텐츠 제공 업체 제외), 호출합니다."입니다. 서비스가 시작되지 않기 때문에 실제로 앱이 종료 될 때 시작됩니다 (이상한 경우). 이것은 액티비티의 onCreate
메서드로 호출을 이동하면 작동하지만, 서비스를 중지 할 수도 있기 때문에 이상하지 않습니다. 앱 수명주기 동안 서비스를 실행하고 싶습니다. 즉, 앱이 시작될 때 서비스가 시작되고 앱이 종료 될 때 서비스가 중지되어야합니다. 이게 말이 돼? 이것을 할 또 다른 방법이 있습니까?
'onCreate'메소드가 Application 클래스에서 호출 되었습니까? 아니요? –
예, 메소드가 호출됩니다! 그러나 이것과 @ vanste25의 대답은 내가 백그라운드에서 실행중인 서비스와 관련이 있다는 것을 깨닫게했습니다. 이 방법은 앱을 두 번째 실행해도 호출되지 않는 것 같아서이 말을합니다. –