Search code examples
androidmultithreadinghandlerbackground-service

Thread running in background returns strings to handler of whatever current activity


I am trying to receive messages in current activity instead of just MainActivity background service: MainActivity.bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget(); MainActivity: here is handler which receive messages from background service. now what I am trying to do is how to make able all activities which I want to revieve messages from this background service. any help will be really appreciated. 'currentActivity'.bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();


Solution

  • Kindly make this class,

    public class MyApp extends Application {
    MyService myService;
    
    @Override
    public void onCreate() {
        super.onCreate();
        myService = new MyService(this);
        mServiceIntent = new Intent(getCtx(), myService.getClass());
        }
    }
    public Context getCtx() {
        return ctx = this;
    }
    

    and receive the message in all activities like wise MainActivity