Skip to main content

Introduction and Example of Eventbus in Android.

EventBus is a library of Greenbot . This is very simple for all users how to implement and its basic use. Here I will also share examples for better understanding. What is EventBus? EventBus is subscribed/publish event bus to communication between components with limited lines of code, in short, its bus for events transfers the data between one activity(class) to another to reflect the changes based on our needs. That makes the data exchange between components like Activity, Fragment, Services, and any kind of backgrounds threads pretty easy. EventBus can handle the threading for us: events can be posted in threads different from the posting thread. Common use can be dealing with UI changes. In Android, UI changes must be done in the Main(UI) thread. In other words, networking or more time-consuming task cannot be handled in the Main thread, for that we must use AsyncTask or Handler to make it more smooth. Now we have one more options use EventBus to change the UI po

Android Client Connected with Socket Server



I hope this will help you for how to start communication between Socket Server (Desktop Chat Server Application) and Mobile Application (Android Socket Client).
This application simply look like as chat application. You can add your idea and innovation to make very useful application.

Follow below steps to run application:
  1. Use custom port and define same port in both application (i.e. Port No: 5657 )
  2. Define computer IP Address in Android Client Application. (i.e. 192.168.1.x). If you want to run this application in real device, then you can't connect with Desktop Socket Server. For that you need to upload runnable jar file in specific server and then you can use that server IP Address.
  3. First start Socket Server Application using Start button.
  4. Now you can start Android application.
  5. Just Enjoy it !!!
Here I give you screen shot for communication between both application.




Socket Client in Android Swing - Prashant Adesara
[Socket Server (Desktop Application)]



Implement Socket Server in Android - Prashant Adesara
[Socket Client (Android Application)]


You can download full source from here.

 1. SocketAndroidClient (https://github.com/prashantadesara/SocketAndroidClient)  
 2. SocketServer (https://github.com/prashantadesara/TCPSocketServer)  

If you are satisfied then please give your comments.
Thanks for you to spend your valuable time.

Comments

  1. Hi Prashant,

    Thank you for this! I am using the Android emulator and I got this to work. However, the code for the SocketServer comes in a .jar executable -- I cannot seem to find the source code. Would you mind sharing?

    Thanks

    ReplyDelete
    Replies
    1. Hi Tarif,

      Ya right, I will upload full source code into same directory very soon. Right now I am changing some part in that.
      Don't worry you will get source code from same link ASAP.

      Thanks for your interest!!!

      Delete
    2. Thank you so much! I've been trying to get an Android-to-Desktop socket connection working for a whole week for a project I'm working on -- thank you :)

      Delete
    3. I committed my code. Now you can access full source code.

      Delete
  2. Hello Prashant!

    Your tutorial is quite good. Can you provide me the same code with the modification that when I close the client and reopen the client, while the server is still running then the server and client should resume their chat. its urgent

    ReplyDelete
    Replies
    1. Hello Aqsa,

      You need to create that kind of tutorial using my example. You use your logic with when someone logout from Android then save chat history and when its login display those history.

      Delete
  3. Hey Prashant!
    I tried it too much but could not succeed. If you say I can provide you my modified code. Please help me if you can. I urgently need this.

    ReplyDelete
    Replies
    1. Hi Aqsa,

      Which problem you facing? Can you please elaborate your issue?

      Delete
    2. I can email u if you provide me your email id. In that way you will be able to see my modification in the code and I hope u can guide me as well.

      Delete
  4. Hi prashant,
    Thanks for posting the code, really helpful. I have a small problem, I was not able to find the project file in the SocketAndroidClient folder to run the project in eclipse. I tried to create a new project
    Can I get the complete project (including the project file).?

    ReplyDelete
    Replies
    1. I tried to create new project, but it is giving some run-time errors. I appreciate if you post the project file as well. Thank You.

      Delete
    2. Hi Satya,

      I have attached zip file with .classpath and .project files. Please download it and try it.
      projectfiles.zip place under this below path.
      https://github.com/prashantadesara/SocketAndroidClient/

      Delete
  5. Hi Prashanth,

    How do I do it for UDP instead of TCP???

    ReplyDelete
    Replies
    1. Hi Pradeepbabu,

      I would suggest you to please refer below link for UDP connection.
      http://www.anddev.org/udp-networking_-_within_the_emulator-t280.html

      Thanks for your interest in my blog.

      Delete
  6. Hi prashant,
    i really really appreciate your work! Well done! Thx

    ReplyDelete
    Replies
    1. Hi Bhellmann,

      Thanks for your interest and comment in my blog.

      Thanks,

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Both the client and server starts but it does not transfer message from one to another.. is there some kind of connection issue?

    ReplyDelete
    Replies
    1. Hi,

      You got any exception or something any error?
      Are you trying into Emulator or Real Device?

      Delete
  9. Hello Prashant!

    Your tutorial is very good to understand but Both the client and server starts but it does not transfer message from one to another on real device.I think i need "runnable jar for specific server" as you told.But i have not good knowledge for done it.Can you provide that jar for server and how to use it simply...
    Thank you very much

    ReplyDelete
    Replies
    1. Hi Ashish,

      I already posted this article with full source code of Android as well as Java Desktop Application.
      Then also I will give you link here. You can also find at the end of every post.
      1. SocketAndroidClient (https://github.com/prashantadesara/SocketAndroidClient)
      2. SocketServer (https://github.com/prashantadesara/TCPSocketServer)

      Thanks for your interest!!!

      Delete
    2. I'll do something wrong previously but now it's working fine...
      Thank you very much.
      I don't have knowledge about desktop app.
      can you provide some installable code that runs in background on window machine and if user send text using android client than notification appear on window machine this will help me lot..

      Delete
  10. sorry for my spell mistake...

    ReplyDelete
  11. Thanks for your tutorial .. And please help me ,i have some error in android pgm.
    "java.lang.NoSuchFieldError: android.os.AsyncTask.THREAD_POOL_EXECUTOR"

    ReplyDelete
    Replies
    1. I though that that error is due to low version of Android Version. Which version you are targeting to run the project?
      Please select Android 4.x +.

      Delete
  12. Hi Prashant..I am a newbie in android..and don't even know how to connect Android client with server. But I feel your project interesting.

    Can you please say me which technology is used for server connection in Android? PHP or advanced java?..If you post the answer, it will be very helpful for me.

    ReplyDelete
    Replies
    1. Hi Krupal,

      Thanks for you interest on my blog.
      In this example I used Java technology on both side server as well as client. I make server as Swing Java Program and Android app as Client.
      First you need to start the server using click on START button. (See above Swing program Image.) After that you can run your android app to established the connection between server and your app.

      Delete
    2. Hi Prashant,
      Thanks for your reply.

      I am thinking to make a web application with cloud connection and GPS tracking in android. Can you explain which technology should I use to establish server side connection.

      Delete
    3. Hi Krupal,

      Yes its possible and you can manage through JSP + Servlet (Web Application) to start and stop your server.
      Finally, Upload your application into Apache Tomcat server and than test it.

      Delete
  13. Hello Sir... I like Your Work.. But If I want to use this on my mobile phone than ?? this is not working. both devices are on same network.. and can i use with mobile data connection and changing to external ip of computer ?? is that work ??

    ReplyDelete
  14. could you tell me how to run the client program in androidstudio

    ReplyDelete
  15. Searched a long time to find a tutorial which provides a solid connection. Your does! 100% stable, no data gets lost! Awesome.
    Thanks so much, bro!

    ReplyDelete
    Replies
    1. Hi Martin,

      Thanks for your interest and such a nice comments.
      I am believe in sharing knowledge to everyone. And I am happy to say that some of you guys got the exact code which I posted on my blog.
      For this example, I searched a very long time and finally make this example and decided to give you for all person those who are not able run.

      Thanks again,
      Prashant.

      Delete
  16. Seriously awesome man :) Really did not find so good tutorial anywhere :)

    ReplyDelete
    Replies
    1. Thanks for your comment and reading my blogs.

      Delete
  17. how csan i get my ip address

    ReplyDelete
  18. Replies
    1. You can manage through JSP + Servlet (Web Application) to start and stop your server.
      Finally, Upload your application into Apache Tomcat server and than test it.

      Delete
  19. bro i wanna get the text from the client and then open notepad that is if the client sends notepad server should open notepad by comparing it with the string

    ReplyDelete
  20. Hi Prashant,
    I want to run this in real device. Can you please tell me step by step what I should do to run this in real device? Is it required another server?

    ReplyDelete
    Replies
    1. Hi Harshitha,

      Yes you can able to run in live server. But for that you need to create runnable jar file and upload into your server. Than use that server's IP in your Android Client Application. (I already given hint on 2nd point on same blog post).

      Thanks for your interest.

      Thanks,
      Prashant.

      Delete
  21. HI, Using above code how we can do one(server) to many(client) communication.

    Generally we aware of that Chat concept. how to achieve using code.

    Ex. Sanjay(client) send request and Shyam(client) send a request
    Now Server accept both request then Server how to identify which client have requested. so response only them. at smae time

    ReplyDelete
    Replies
    1. You can handle by Database at Server side to identify by userid and than response to specific users.

      Delete
  22. can i do an android to android chat from this app. i dont want to use windows as server. please do share some resources about that.

    snkumar.nayak@gmail.com

    ReplyDelete
  23. can i live data show on android apps from desktop application

    ReplyDelete
    Replies
    1. It would be better to use Push Notification, its having many features, You can go with GCM(FCM) for notification, just calling API end point from your Desktop application and all connected device with FCM will get notified by your applied logic.

      Delete
  24. Does it working for multiple android clients and one server ?

    ReplyDelete
  25. Hi Prashant I have an query I have an websocket server running as a module and want to establish a websocket / tcp client in android app for that if i only use TCP client source it will work .. I just want to send string from client to server to perform some functionlity. Kindly suggest me

    ReplyDelete
    Replies
    1. You mean to send String from your Android Device to Server? Can you please clarify in details so I can get better idea what you want to do?

      Delete
  26. 75F19Kimberly5D5BC15 April 2024 at 19:28

    BF660
    ----
    ----
    ----
    ----
    ----
    ----
    ----
    ----
    matadorbet

    ReplyDelete

Post a Comment

Popular posts from this blog

Disable Force Stop in Android

I have worked since 1 week on how to disable the FORCE STOP button in Manage Application in Android. And I was able to disable the Force Stop and Uninstall button in Manage Application. For that you need to make your application as Device Administrator. Android has add this kind of feature from Android 2.2 version. This feature is for Security reason with different option. Set Minimum Password Strength, Data Wipe, Force Lock,  Reset Password and so on. We set our application as Device Administrator to change above features. For more info and advance example you can check your android-sdk path (>= android-8). \samples\android-[>=8]\ApiDemos\src\com\example\android\apis\app\DeviceAdminSample.java See the below screen shots which shows how to activate the application and disable force stop button. [Active device adminstrator] [Disable the Force Stop and Uninstall button] Follow the below steps to disable the FORCE STOP button. 1. Create one cl

Introduction and Example of Eventbus in Android.

EventBus is a library of Greenbot . This is very simple for all users how to implement and its basic use. Here I will also share examples for better understanding. What is EventBus? EventBus is subscribed/publish event bus to communication between components with limited lines of code, in short, its bus for events transfers the data between one activity(class) to another to reflect the changes based on our needs. That makes the data exchange between components like Activity, Fragment, Services, and any kind of backgrounds threads pretty easy. EventBus can handle the threading for us: events can be posted in threads different from the posting thread. Common use can be dealing with UI changes. In Android, UI changes must be done in the Main(UI) thread. In other words, networking or more time-consuming task cannot be handled in the Main thread, for that we must use AsyncTask or Handler to make it more smooth. Now we have one more options use EventBus to change the UI po