2010-06-29 4 views
1

인 호출 활동으로 서비스가 다시보고 될 수 있습니까? 서비스가 특정 처리 단계에 도달했을 때 호출 활동으로보고 할 수있는 방법이 있습니까?Android - 상태가

예 : Android 서비스로 백그라운드에서 실제 음악 재생을 시작하는 음악 플레이어 활동을 고려해보십시오. 서비스가 Mediaplayer 's onPrepared에 도달하면 Activity를 감지하고 알려줍니다. MediaPlayer의 onPrepared가 호출 될 때 서비스에서 호출 작업을 알 수있는 방법이있어 오디오에 준비가되어 재생 준비가되었음을 액티비티에 알릴 수 있습니까?

기본적으로 서비스가 준비된 시간에 도달했는지 확인하기 위해 끊임없이 pinging을하는 대신 작업에 스레드가있는 지 확인하고 있습니다.

감사 크리스

답변

0

당신은 항상 activitie (들)와 서비스 간의 통신 인터페이스를 사용할 수 있습니다. 서비스에 연결 한 직후에 인터페이스를 설정하고 서비스가 해당 활동으로 다시 통신하기를 원하는 상태가되면 적절한 방법을 호출합니다.

2

내가 찾은 가장 쉬운 방법은 지원 라이브러리에서 사용할 수있는 LocalBroadcast를 사용하는 것입니다. 서비스에서

: 활동에

String action = "status"; // arbitrary string 
private void updateStatus(){ 
    Intent intent = new Intent(action); 
    // fill the intent with other variables you want to pass 
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 
} 

:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    LocalBroadcastManager.getInstance(context).registerReceiver(receiver,new IntentFilter(action)); 
} 


@Override 
protected void onResume() { 
    LocalBroadcastManager.getInstance(context).registerReceiver(receiver,new IntentFilter(action)); 
} 

@Override 
protected void onPause() { 
    LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver); 
} 

String action = "status"; 
BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String intentAction = intent.getAction(); 
     if(intentAction.equals(action){ 
      // Process the status from the service 
     }    
    } 
}; 
다음은 그 예이다
관련 문제