2012-05-28 2 views
1

Service으로 안드로이드 장치에서 실행되는 서버가 있습니다. 사용자가 클라이언트 번호를 입력하고 서비스가 시작되기를 원합니다. 액티비티에서 서비스로 데이터를 전달하는 대신에 이것을 처리하는 더 좋은 방법이 있습니까?서비스를 시작한 후 활동이 중단됩니다.

위의 방법을 사용할 때 클라이언트가 멈추고 응용 프로그램이 강제로 종료되는 것으로 보입니다. 여기

String a = clients.getText().toString(); 
Bundle bundle = new Bundle(); 
bundle.putCharSequence("NumberOfClients", a);   
Intent intent = new Intent(Manage.this, Server.class); 
intent.putExtras(bundle); 
Log.d("hi", a); 
startService(intent); 

그리고 서버입니다 : 다음은 코드가 나는 클라이언트가 응답하여 서버에 연결 버튼을 누르면

@Override 
public void onCreate() 
{       
    Thread server = new Thread(new ServerThread()); 
    server.start();     
} 

public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId);  
    Bundle bundle = new Bundle(); 
    bundle = intent.getExtras(); 
    numberofclients = (String) bundle.getCharSequence("NumberOfClients"); 
    int a = Integer.parseInt(numberofclients);  

    return a; 
    } 

. 왜 이런 일이 일어나는 걸까요?

+0

로그, 추적 또는 디버깅을 참조하여 무슨 일이 일어 나는지 – Snicolas

답변

2

onStartCommand는 서비스를 처리하는 방법에 대해 미리 정의 된 값을 반환해야합니다. START_NOT_STICKY을 (를) 원할 것으로 보입니다. 따라서 onStartCommand의 신고서에 반환 할 내용입니다. 즉

return START_NOT_STICKY; 

정수 a를 서비스의 다른 곳에서 사용하려면 전역 변수를 만들어 a와 동일하게 설정하십시오.

서비스에 안드로이드 문서 도구에 http://developer.android.com/reference/android/app/Service.html#START_CONTINUATION_MASK

관련 문제