0
나는 AsyncTask를 가지고 있으며 백그라운드에서 url에서 텍스트를 가져 오기 위해 배우는 서비스를 시작하려고합니다.서비스에서 AsyncTask로 매개 변수를 가져 오는 방법
작업이 끝나고 서비스에서 텍스트를 가져 와서 AsyncTask를 계속 진행해야합니다. 하지만 서비스에서 AsyncTask로 매개 변수를 가져 오는 방법을 알았고 완료되었는지 확인하지 못했습니다.
AsyncTask를 내 코드는 다음과 같습니다
protected String doInBackground(String... params) {
//starts service number activite
Intent serviceIntent = new Intent();
serviceIntent.setAction("services.conServise");
context.startService(serviceIntent);
return null;
}
서비스 코드 : 돕는
public void onStart(Intent intent, int startId) {
// Create some random data
try{
URL url = new URL("http://hostname:80/index.html");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
str is one line of text; readLine() strips the newline character(s)
}
activitNum = Integer.parseInt(str);
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
statusCon=-1;
}finally{stopSelf();}
super.onStart(intent, startId);
}
감사합니다!
그리고 앱이 백그라운드로 진행된다면? –
질문에 당신은 서비스가 반환 될 때까지 메소드'doInBackground()'를 멈추고 싶다고 말했습니다. 이제 왜 비 UI 백그라운드 스레드를 멈추고 싶습니까? UI를 차단하지 않기 때문에 실행하는 데 어느 정도 시간이 걸릴 수 있습니다. 앱이 백그라운드로 이동 한 후에'Service'를 실행하려면'onPostExecute()'에서'Service'를 별도로 시작할 수 있습니다. –
은 Application 클래스로 해결했습니다. 감사 –