아래 서비스를 확장하는 MyService라는 클래스가 있습니다. 모든 것이 실행될 때까지 Thread의 run 메소드에서 Toast.makeText...
행을 제거합니다.안드로이드에서 Service 클래스의 Thread로부터 Activity의 UI 컴포넌트에 접근하기
왜? 그리고 Thread 클래스의 run 메서드에서 Activity 구성 요소에 액세스하려면 어떻게해야합니까?는
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public void onCreate() {
Toast.makeText(this, "This msg will be shown", Toast.LENGTH_LONG).show();
Log.d("Bilgi", "This msg will be shown.");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "This msg will be shown", Toast.LENGTH_LONG).show();
super.onStart(intent, startId);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Log.d("This msg will ","be shown"); //if I remove next line
Toast.makeText(this, "This msg will NOT be shown", Toast.LENGTH_LONG).show();
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 5000, 8000);
}
[바인더 클래스 확장] (https://developer.android.com/guide/components/bound-services.html#Binder)을 통해 바운드 서비스를 사용할 수 있습니까? 안드로이드 문서는 "당신의 서비스가 단지 당신의 어플리케이션을위한 배경 작업자 일 때"Messenger를 사용하는 것보다이 방법을 제안하는 것으로 보인다. – AST
@AST : 그것은 이벤트 버스처럼 다른 옵션 일 것이다. – CommonsWare