Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. how to send data from service class to main activity class

how to send data from service class to main activity class

Scheduled Pinned Locked Moved Android
designtutorialquestion
2 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mAzeem22
    wrote on last edited by
    #1

    i am developing a call recorder application..in this the recording done by background service class now how can i receive the call info in main activity to display on UI.

    N 1 Reply Last reply
    0
    • M mAzeem22

      i am developing a call recorder application..in this the recording done by background service class now how can i receive the call info in main activity to display on UI.

      N Offline
      N Offline
      nfsoft
      wrote on last edited by
      #2

      You should use and Handlers... Not so easy to implement.

      //ON SERVICE SIDE
      public class YourService extends Service {
      public static final int SAY_HELLO = 1;
      public static final int REPLY_HELLO = 2;

      private int cnt = 0;

      class IncomingHandler extends Handler {
      @Override
      public void handleMessage(Message msg) {
      switch (msg.what) {
      case SAY_HELLO:
      msg.replyTo.send(Message.obtain(null, REPLY_HELLO, cnt++, 0));
      break;
      }
      }

      /**
      * Target we publish for clients to send messages to IncomingHandler.
      */
      final IncomingHandler mHandler = new IncomingHandler();
      final Messenger mMessenger = new Messenger(mHandler);

      }

      //ON CLIENT SIDE

      ...
      final Messenger mMessenger = new Messenger(new IncomingHandler()); //You can do it...
      private Messenger mBoundService;

      private ServiceConnection mConnection = new ServiceConnection() {
      public void onServiceConnected(ComponentName className, IBinder service) {
      mBoundService = new Messenger(service);
      mIsBound = true;
      try{
      Message msg = Message.obtain(null, 1);
      msg.replyTo = mMessenger;
      mBoundService.send(msg);
      } catch (RemoteException ex){
      ex.printStackTrace();
      }
      }

          public void onServiceDisconnected(ComponentName className) {
          	mBoundService = null;
              Toast.makeText(context, "Connection GONE", Toast.LENGTH\_SHORT).show();
          }
      };
      
      
      public void doBindService() {
      	Intent intent = new Intent("com.yourpackage.YourService");
      	if (!context.bindService(intent, mConnection, Context.BIND\_AUTO\_CREATE)) {
      	    try { 
      	        context.unbindService(mConnection);
      	    } catch (Throwable t) {}
      	    // Clean up 
      	    return;
      	}
      }
      
      public void doUnbindService() {
          if (mIsBound) {
          	Message msg = Message.obtain(null, MESSAGE\_UNREGISTER\_CLIENT);
          	msg.replyTo = mMessenger; 
          	try{
          		 mBoundService.send(msg);
          	 } catch (RemoteException ex){
      	     }
              context.unbindService(mConnection);
              mIsBound = false;
          }
      }
      

      So Sry... Im not so good helping ppl. Just wanted to share this, becouse it has been hard for me to.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups