Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:13:42+00:00 2026-05-25T06:13:42+00:00

I am implementing an application which is kind of VOIP application. So my application

  • 0

I am implementing an application which is kind of VOIP application. So my application is kind of network application. Now I want to implement two part in my application, one is GUI part and one is network part. My GUI part will just contain activities and handling of user interaction. My Network part should handle all network related activities like handling incoming network data and sending data to network based on GUI interaction. Now whenever there is any incoming data, I want to update some activity whose reference is not there in Network module. So what could be the best way to update activity from some other class? In my case some other class is my Network class. So in short I would like to ask that what should be the architecture in such scenario? i.e. Network part should run in separate thread and from there it should update GUI?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:13:43+00:00Added an answer on May 25, 2026 at 6:13 am

    I have written apps like this, and I prefer the Handler method. In fact I have written an Abstract Activity class to do all the hard work and simply extend it in any activity that want to be notified of a change.

    To Use the following code, just get your Activity to extend UpdatableActivity and override the dataUpdated() method. This method is called when your Service notifies the handler that data has been updated. In the Service code put your code to do an update in the update() method (Or modify to call your existing code). This allows an activity to call this.updateService() to force an update. The service can call the sendMessageToUI() method to notify all interested activities that the data has been updated.

    Here is what the abstract activity looks like:

    public abstract class UpdatableActivity extends Activity {
    
    public static final String TAG = "UpdatableActivity (Abstract)";
    private final Messenger mMessenger = new Messenger(new IncomingHandler());
    private Messenger mService = null;
    private boolean mIsBound;
    
    
    protected class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            if (Constants.LOG_DEBUG) Log.d(TAG, "Service has notified us of an update: ");
            switch (msg.arg1) {
            case UpdateService.MSG_DATA_UPDATED:
                dataUpdated();
                break;
            default: super.handleMessage(msg);
            }
        }
    }
    
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);
            try {
                Message msg = Message.obtain(null, UpdateService.MSG_REGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);
            } catch (RemoteException e) {
                // In this case the service has crashed before we could even do anything with it
            }
        }
    
        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
            mService = null;
        }
    };
    
    
        /**Override this method in you acctivity to handle the update */
    public abstract void dataUpdated();
    
    void doBindService() {
        if (Constants.LOG_DEBUG) Log.d(TAG, "Binding to service...");
        bindService(new Intent(this, UpdateService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }
    
    void doUnbindService() {
        if (mIsBound) {
            // If we have received the service, and hence registered with it, then now is the time to unregister.
            if (mService != null) {
                try {
                    Message msg = Message.obtain(null, UpdateService.MSG_UNREGISTER_CLIENT);
                    msg.replyTo = mMessenger;
                    mService.send(msg);
                } catch (RemoteException e) {
                    // There is nothing special we need to do if the service has crashed.
                }
            }
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    
    public void updateService() {
        if (Constants.LOG_DEBUG) Log.d(TAG,"Updating Service...");
        if (mIsBound) {
            if (mService != null) {
                try {
                    Message msg = Message.obtain(null, UpdateService.MSG_SET_INT_VALUE, UpdateService.MSG_DO_UPDATE, 0);
                    msg.replyTo = mMessenger;
                    mService.send(msg);
                } catch (RemoteException e) {
                    if (Constants.LOG_ERROR) Log.e(TAG,Log.getStackTraceString(e));
                }
            }
        } else {
            if (Constants.LOG_DEBUG) Log.d(TAG, "Fail - service not bound!");
        }
    }
    
    pu
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.doBindService();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            doUnbindService();
        } catch (Throwable t) {
            if (Constants.LOG_ERROR) Log.e(TAG, "Failed to unbind from the service", t);
        }
    }
    }
    

    And here is what the Service looks Like:

    public class UpdateService extends Service {
    
    public static final String TAG = "UpdateService";
    
    public static final int MSG_DATA_UPDATED = 0;
    public static final int MSG_REGISTER_CLIENT = 1;
    public static final int MSG_UNREGISTER_CLIENT = 2;
    public static final int MSG_DO_UPDATE = 3;
    public static final int MSG_SET_INT_VALUE = 4;
    
    private static boolean isRunning = false;
    
    private Handler handler = new IncomingHandler();
    private final Messenger mMessenger = new Messenger(handler);
    
    private ArrayList<Messenger> mClients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
    
    
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
    class IncomingHandler extends Handler { // Handler of incoming messages from clients.
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_REGISTER_CLIENT:
                mClients.add(msg.replyTo);
                break;
            case MSG_UNREGISTER_CLIENT:
                mClients.remove(msg.replyTo);
                break;
            case MSG_SET_INT_VALUE:
                switch (msg.arg1) {
                    case MSG_DO_UPDATE:
                        if (Constants.LOG_DEBUG) Log.d(TAG,"UI has asked to update");
                        update();
                        break;
                }
                break;
            default:
                super.handleMessage(msg);
            }
        }
    }
    
     private void sendMessageToUI() {
         if (Constants.LOG_DEBUG) Log.d(TAG, "Notifying "+mClients.size()+" UI clients that an update was completed");
            for (int i=mClients.size()-1; i>=0; i--) {
                try {
                    // Send data as an Integer
                    mClients.get(i).send(Message.obtain(null, MSG_SET_INT_VALUE, MSG_DATA_UPDATED, 0));
    
    
                } catch (RemoteException e) {
                    // The client is dead. Remove it from the list; we are going through the list from back to front so this is safe to do inside the loop.
                    mClients.remove(i);
                }
            }
        }
    
     public static boolean isRunning()
        {
            return isRunning;
        }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        isRunning = true;
        if (Constants.LOG_DEBUG) Log.d(TAG, "Service Started");
        update();
    }
    
    
    @Override
    public void onDestroy() {
        if (Constants.LOG_DEBUG) Log.d(TAG, "Service Destroyed");
        isRunning = false;
    }
    
    private void update() {
    /**Your code to do an update goes here */
    }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing an application which want to draw lines in the panel. But the
I'm implementing a Rails application in which users will be able to store snippets
I have an application on which I am implementing localization. I now need to
I am implementing an application in which I play two sounds ( touchandshow followed
I am implementing a database application and I will use both JavaDB and MySQL
I have an application which consists of two activities/screens and a java class from
I am implementing game application.In which i have set UIView on camara overly.When i
I am implementing game application.In which there is some mosquito(object).They moves randomly on the
I'm implementing a wpf application which display a list of items, and provides the
I'm building an enterprise application which has fraud rules. The rules will be based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.