2017-04-15 4 views
0

내가 서비스를 실행 한 호출되지 onServiceConnected (바인더 제본 successfuly 이전에 를 시작하고 주요 활동과 unbinded) :안드로이드가 : 이미 서비스를 실행하는 바인딩 할 수 없습니다,

Intent startingIntent = new Intent(this, SturkoPlayerService.class); 
startingIntent.setAction(SturkoPlayerService.PLAYER_START); 
bindService(startingIntent, connection, Context.BIND_AUTO_CREATE); 

때 나는 successfuly 가까운 mainactivity (서비스 unbinded입니다 그리고 계속 실행) 및 알림 의도 mainactivity 다시 할 내 실행중인 서비스를 결합하려고 다음

Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
intent.putExtra("Service", 1); 
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //tried also another flags 
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

,
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ... 

    if (getIntent() != null && getIntent().hasExtra("Service")) { 
     if (!bound) { 
      bindService(new Intent(this, SturkoPlayerService.class), connection, 0); 
     } 
    } 
} 

Connection 변수의 onServiceConnected 기능은

private ServiceConnection connection = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service; 
     sturkoService = mBinder.getService(); 
     bound = true; 
     sturkoService.registerClient(MainActivity.this); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     bound = false; 
    } 
}; 

매니페스트 호출되지 입니다 :

... 
<activity android:name=".MainActivity" 
    android:screenOrientation="portrait" 
    android:launchMode="singleTask"> //tried other modes too 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<service 
    android:name=".Service.SturkoPlayerService" 
    android:enabled="true" 
    android:exported="true" 
    android:singleUser="true"> 
</service> 
... 

PS : Catlog은 보이지 않았다 뭔가를해야만

+0

"service is unbinded and running running"- 당신이'startService()'를 호출하지 않으면 서비스가 계속 실행되지 않습니다. 'bindService()'호출에서'Context.BIND_AUTO_CREATE' 호출을'0 '으로 변경하면, 이제는 문제가 생길 수 있습니다. 문제는 당신이 원래 바인딩 해제했을 때 서비스가 파괴되었다는 것입니다. – CommonsWare

+0

아니요, 서비스가 계속 실행됩니다 (소리를들을 수 있습니다). 내가 그것을 만들 때 Context.BIND_AUTO_CREATE (첫 번째 코드 블록)를 사용하고 다시 바인딩 할 때 0을 사용합니다. – Johnny

+1

"소리가 들립니다."- 서비스가 사운드를 재생하지 못합니다. Android가 사운드를 재생 중입니다. 소리가 들리다고해서 서비스가 실행 중임을 의미하지는 않습니다. "내가 그것을 만들 때 Context.BIND_AUTO_CREATE (코드의 첫 번째 블록)를 사용하고 다시 바인딩 할 때만 0을 사용합니다. ** '0'을 'Context.BIND_AUTO_CREATE'로 바꾸어보십시오. 이제 바인딩되면 문제는 서비스가 원래 바인딩 해제되었을 때 서비스가 파괴되었으며 서비스에 버그가있어 안드로이드가 일단 소리가 재생되지 않으면 멈추지 않게하는 것입니다. – CommonsWare

답변

0

서비스 것입니다 만 다음과 같은 경우에 실행하십시오 :

  • 는 일단

그렇지 않으면 (예를 들어, stopSelf(), stopService()를) 중지 1+ 바인드 클라이언트, 또는

  • 당신이 그것에 startService()에게 전화를 아무것도가 있습니다 마지막으로 바인드 된 연결이 Y 인드되어 있으면 서비스가 중지됩니다.

  • 관련 문제