0


@first Sorry for my bad english.
I have created a own Listener. I want to change a TextView, when the Listener is called in the MainActivity from a Service. The idea for my own Listener is from:

http://tseng-blog.nge-web.net/blog/2009/02/17/how-implement-your-own-listener-android-java/

In the Code Example the TriggerMethod() ist called from a Calculation Thread, running in the Service. I solved the Problem, but I find, it isn't pretty nice, because in every new Activity I have to make a new Thread. Is it possible to create an interface/listener that automatically can change the UI?
Used to solve the Problem:
http://developer.android.com/guide/components/processes-and-threads.html

ResultListener.java:

public interface ResultListener {   
    public void onResultAvailable(double result);
}

SimuService.java:

public class SimuService extends Service {

    private ResultListener mResultListener = null;

    public void setResultListener(ResultListener listener){
        mResultListener=listener;
    }
  public void triggerMethode(){
        observeResultDouble=getObserveDouble;
        mResultListener.onResultAvailable(observeResultDouble);         
    }

MainActivity:

public class MainActivity extends FragmentActivity{
    TextView txtView;
    ResultListener mResultListener;
    SimuService mSimuService;

    protected void onCreate(Bundle savedInstanceState) {
        txtView = (TextView) findViewById(R.id.txtServiceTime);
        //Create Service .....an Bind
        mResultListener = new ResultListener() {            
            @Override
            public void onResultAvailable(double result) {
                txtView.setText("Result: "+result);
            }
        };
        mSimuService.setResultListener(mResultListener);

    }

MY SOLUTION:

ResultListener = new ResultListener() {         
            @Override
            public void onResultAvailable(double result) {
                this.result=result;
                runOnUiThread(setNewDataToUI);  
            }
        };

private Thread setNewDataToUI = new Thread(new Runnable() {

    @Override
    public void run() {
        txtView.setText("Result: "+result);

    }
});
Tobias
  • 3
  • 3

1 Answers1

0

First of all: If you reference a Service in an Activity, the Service becomes pretty much useless. The advantage of services are, that they are loose coupled and can work indepenendtly form activities (=what the user sees) and its lifecycle and might even be in their own process. Thus activity-service communication is through intents or inter-process language AIDL, not through callbacks. If you want something executed asynchronosly use AsyncTask.

To your main problem: as you found out, you can only modify the UI on the UI-thread. So by design, leave changing UI in the component, thats responsibly for that (either activtiy or fragment), that will prevent the need of runOnUiThread()

Your code seems like txtView.setText("Result: "+result); will be executed in the Activity, but it wont. It will be executed in the Service, which (as I impleied before) does not run on the UI-thread. The problem is, I dont get the intent, what exactly you want to achieve so it is hard to give you an alternative solution.

Patrick
  • 33,984
  • 10
  • 106
  • 126
  • Ok. I have to change the communication between the Service and Activity. I believe this link would solve my Problem: http://stackoverflow.com/questions/4908267/communicate-with-activity-from-service-localservice-android-best-practices Thank you for your answer. – Tobias Jun 14 '13 at 07:36