0

startService()로 시작한 다음 bindService()로 바인딩하는 원격 서비스가 있습니다. 사용자가 스 와이프로 액티비티를 닫더라도 앱이 명시 적으로 앱을 중지 할 때까지 서비스가 백그라운드에서 실행되기를 원하므로이 작업을 수행합니다. 내가 디버거에서이 프로그램을 실행할 때시작된 서비스에 바인딩

<service 
     android:name=".TrackService" 
     android:process=":track_service_process" 
     android:stopWithTask="false" 
     android:label="@string/track_service_label" 
     android:exported="false" 
     android:enabled="true"> 
     <intent-filter> 
      <action android:name="com.mydomain.gpslogger.TrackService" /> 
     </intent-filter> 
    </service> 

: 여기

public class TrackService extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i("onStartCommand","got to onStartCommand"); 
     return START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.i("onBind", "got to onBind"); 
     return trackServiceBinder; 
    } 
} 

활동한다 : 여기

public class GPSLoggerActivity extends Activity implements ServiceConnection { 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     Intent intent = new Intent(TrackService.class.getName()); 
     intent.setClass(getApplicationContext(), TrackService.class); 

     startService(intent); 
     Log.i("onStart", "started TrackService"); 

     if (!getApplicationContext().bindService(intent, this, 0)) { 
      // close the Activity 
     } 

     Log.i("onStart", "bound to TrackService"); 
    } 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     Log.i("onServiceConnected", "got here"); 

     // get and use the interface from the Binder 
    } 
} 

이 서비스의 매니페스트입니다 여기에 서비스의 관련 부분은 startService() 및 bindService()를 통해 단계별로 모든 것이 좋습니다. 서비스 내에서 onStartCommand()가 호출 된 다음 onBind()가 호출되고 Activity에서 onServiceConnected()가 Binder와 함께 호출됩니다. 그러나 디버깅하지 않을 때 어떤 이유로 서비스의 onBind() 메서드가의 onStartCommand() 메서드보다 먼저 이 호출됩니다. 로그의 결과는 다음과 같습니다.

11-28 23:17:01.805: I/onStart(1103): started TrackService 
11-28 23:17:01.815: I/onStart(1103): bound to TrackService 
11-28 23:17:01.985: I/onBind(1165): got to onBind 
11-28 23:17:01.995: I/onStartCommand(1165): got to onStartCommand 

Activity의 onServiceConnected() 메소드는 절대로 호출되지 않습니다. bindService() 호출에서 BIND_AUTO_CREATE를 사용해 보았습니다. 효과가 없습니다. 나는 여기에 어떤 종류의 경쟁 조건이 있다고 분명히 말하고 있지만, 나는 이것을 아마도 20 번 실행했고 항상 같은 순서로 나온다. 서비스에서 올바른 호출 순서를 적용하기 위해 생각할 수있는 유일한 방법은 Activity에 BroadcastReceiver를 넣고 서비스의 onStartCommand() 메소드에서 Intent를 보내어 Activity가 bindService()를 호출 할 시간임을 알리는 것입니다). 꽤 괴롭 히는데 ... 더 좋은 방법이 있을까요? 아니면 부재중 전화가 원인이되는 무언가가 있습니까?

+0

서비스가 다른 프로세스로 실행되고 있습니다 ... android : exported = "true"를 사용하여 시도해 보셨습니까? –

+0

영향을주지 않습니다. 내 보낸 속성이 다른 앱이 서비스를 볼 수있게 해준다고 생각합니다. 비록 다른 앱이지만 동일한 앱입니다. 질문에서 제안한 kludge fix를 구현했으며 작동합니다. 아직도 더 우아한 해결책이 있어야하는 것처럼 보인다. –

답변

0

startService()으로 전화 한 다음 일정 시간 동안 기다렸다가 bindService()으로 전화 할 수 있습니다. 얼마나 오랫동안 기다려야 할 것인지는 확실하지만, 시작했을 때 Service의 작업량이 많지 않으면 500 밀리 초 또는 1000 밀리 초의 지연이 발생해야한다고 생각합니다.

나는 이것이 중요한 이유에 대해 질문 할 것입니다. Service에는 onCreate() 메서드가 있으며 이 인스턴스화 될 때 onStartCommand() 또는 이 먼저 호출되는지 여부에 관계없이 호출됩니다. onCreate()에서 중요한 설정을 수행 할 수 있도록 아키텍처를 설정할 수 있어야하며 따라서 onStartCommand() 또는 onBind()이 호출되는 순서는 중요하지 않습니다. 참고 : bindService() 을 호출해야합니다.

또한 onServiceConnected() 메서드가 호출되지 않는 이유를 모르겠습니다. bindService()에 대한 호출이 false (기본적으로 Android에서 Service을 바인딩 할 수 없음을 의미 함)을 반환하지 않는 한 onServiceConnected() 콜백을 가져야합니다. 이상하게 들린다. 하나의 가능성은 onCreate()에서 Service이 충돌하거나 onCreate() 메서드를 실행하는 데 너무 오래 걸리는 것입니다.이 경우 Android에서는 Service이 손상되었다고 선언합니다.

관련 문제