0

I created a Handler in my activity. The handler will be stored in the application object.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.action_activity);
    appData = (AttachApplication) getApplication();

    Handler updateHandler = new Handler() {
        public void handlerMessage(Message msg) {
            Log.d( TAG, "handle message " );
        }
    };
    appData.setUpdateHandler( updateHandler );


}

My plan is that this handleMessage will be called when i setEmtpyMessage in my service. The service retrieves the handler from the application object.

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand of attachService");
    List<Job> jobList = DBManager.getInstance().getAllOpenJobs();
    appData = (AttachApplication) getApplication();
    updateHandler = appData.getUpdateHandler();
            updateHandler.sendEmptyMessage( 101 );

I checked the logs, but there is no handle message so that it seems that my plan does not work. I want to update a textfield each time my service did its job.

Al Phaba
  • 6,545
  • 12
  • 51
  • 83

3 Answers3

0

In Your case You shoild use BroadcastReceiver like this:

define receiver in your Activity class:

public class DataUpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(MainService.REFRESH_DATA_INTENT)) {
                        //do something
        }
    }
}

on your onCreate or onStart method you must register receiver:

DataUpdateReceiver dataUpdateReceiver = new DataUpdateReceiver();
IntentFilter intentFilter = new IntentFilter(MainService.REFRESH_DATA_INTENT);
registerReceiver(dataUpdateReceiver, intentFilter);

on your service add this:

public static final String REFRESH_DATA_INTENT = "done";

and when you done all staff you must send brocast like this:

sendBroadcast(new Intent(MainService.REFRESH_DATA_INTENT));
Dawid Sajdak
  • 3,064
  • 2
  • 23
  • 37
  • Thanks this helped really, with that i can do some stuff in the view again and update my textfields – Al Phaba Dec 18 '12 at 21:40
0

Your code snippet says public void handlerMessage(Message msg), but I think you mean public void handleMessage(Message msg), without the r. You can avoid these problems by using the @Override tag when you intent to override methods from a superclass; so your snippet would be rendered @Override public void handleMessage(Message msg), whereas @Override public void handlerMessage(Message msg) would be an error.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
0

What are you trying to do? I really don't see the point of instantiating a Handler in an Activity, since all you're doing is getting Messages from the MessageQueue. You certainly don't want to fool around with any of the Messages that Android posts, and there are much better ways of sending messages to the Activity.

Of course, you don't include the code for AttachApplication, so I can only speculate.

You're also trying to access this Handler from a Service. Something is going on, but I'm not sure what.

If you want to update a TextView every time your Service does its job, send a broadcast Intent from the Service to the Activity, and use a broadcast receiver in the Activity. You should also consider using an IntentService instead of a Service.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18