How to create a chat screen in Android? - android

I'm creating an Android chat application. I want chat screen to include features like a new UI screen for chat thread with a different user. But I don't know to move further with it. Should I use service or asyntask or is there any other way. Please help me get started.

No. if you use each thread for each user, you will spawn many threads (memory, and time to create and clear...).
You should use a service to listen when receive message (this is an easy work by using intent), and you can do in your main UI Thread.

The chat gets closed (The network connection with the server/another device which the user was chatting with) once the activity closes, so I don't see a reason to use a serivce/AsyncTask. Just have a thread waiting for data from the socket streams, and show that data to the user once it arrives.

Related

Best approach to open a chat activity after friendlist activity at chat app

Let's say I'm making a chat application.
I have an activity FriendListActivity (that shows the list of friends)
this activity also has a Socket that listens to every message that comes from the server.
I also have a ChatActivity that's called whenever I click at a friend within FriendListActivity.
My questions about it:
what happens to FriendListActivity attributes when I call ChatActivity from it? Do they die?
How should I place those activities somehow they work together and the atributes don't die?
I assume that by 'called whenever I click at a friend within FriendListActivity' you mean that you will create an intent and start the new activity, ChatActivity. If this is what you plan on doing, then the answer to your first question is no - FriendListActivity attributes (and any state information) is maintained in that Activity's class (FriendListActivity). What happens is that ChatActivity now becomes the 'top-level' activity and is presented to the user, while the FriendListActivity will run in the background.
See http://developer.android.com/training/basics/firstapp/starting-activity.html for a good description of how to start new activities and communicate information between them.
Generally, for chat applications, you will create a Thread that handles incoming messages (usually by listening on a socket associated with a server). This thread will block on a socket read operation (the exact function name/API will depend on your particular implementation) and wait for the server to send it data. When data is received on the socket, your thread will put that data into some kind of queue (like an 'unread messages' queue) or call a function in the main application to handle the new message. Keep in mind that the UI can only be updated by your application's main thread, so you'll need a way to exchange information from your Socket to the UI handler in order to say, update a TextView object to display the message to the user.
For the Android-way to communicate information between threads, check out https://developer.android.com/training/multiple-threads/communicate-ui.html.
If you have any particular examples/code to share that you're having trouble with, update your question and we can try and provide more specific help.

Android Service with socket programming

Hey I need to implement a infinite loop for receiving commands through socket and updating status in my android app and want that thread to be running even if application is closed. So I need to know that what I'm thinking is optimal solution or not... I am planning to make a service which create a thread which will look for commands and this class is also Observable so the activity can get updates of status from it. Kindly suggest your way or if you think this is right solution. Thanks.
Running things in background is usually a bad idea(battery life), especially if using data services(data plan costs). What you need is indeed a service, but start your updating status thread only when you receive a user present broadcast and stop it when the device goes to sleep(I think it's enough just not to request a wake lock and not use startForeground()).

Android background thread that updates the UI in multiple activities

I'm very new to Android, this is my first app. I'm just looking for someone to point me in the right direction.
I am building an app that will query a server over tcp/ip every minute. I would like to show the status of this server (up/down) at the top of every activity/screen.
My questions are:
1) What should I use to continuously ping a server in the background across multiple activities? For instance, is there a way to spawn a thread from one activity in such a way that it will continue even if the user goes to a different screen?
2) How can I allow this thread to update a status label on whatever screen the user happens to be viewing?
Use services. Please get familiar with documentation first.
You can use service binding or local broadcast messaging to notify your current/front activity about changes from your service

Heavy traffic on server causes app to force close

I have developed an app to communicate with my own server and published it. However, sometimes the app force closes. I know there is no bug in the code because the app works properly most of the time, but sometimes it is waiting for an answer from the server forever. I think this is due to the fact that so many people are using the app, and the app refreshes every 1 second or so, so this heavy traffic causes the server to take a large amount of time to respond. So how do you take care of such a use case? should I have a use case where if the server does not respond after some time you just stop the app and throw a message saying that the server is not responding or something like that?
Right now, your main application is timing out due to server load. If you put your connection details in a thread, you will be able to avoid having that main thread time out. You can check for updated data from the connection thread (through some shared object) and then present a message to the user if the data has stopped.
It sounds like you have your server communication code within your main Activity. Any code running in this Activity will be run in the main UI thread. If your code sends a request to your server, and is then waiting for a response, the main UI thread is blocked until your server responds. The Android OS recognises that the UI thread has effectively hung, and kills your app.
What you need to do is to separate out the UI code in your Activity from the server communication code. You should move this into an AsyncTask or a new Thread/Handler combination. This will allow the UI to remain responsive even when your server is under load.
Documentation for AsyncTask
Designing for Responsiveness
Android Thread Messaging
Thread example

Any way to run a background thread behind multiple Activity and show alert when done?

My application is using a REST based webservice to show multiple activity. To enable the user offline browsing, I am storing the REST data into local SQLiteDatabase with AsyncTask (with some dialogue to wait). However, it takes a long time to load the db (10 mins) and its just bad experience for the user. So I was wondering if there is a way to run the DBLoading thread in background and let the user continue using the app (With multiple activity) with the REST service. When DBLoading thread is done, I want to push a notification in the current Activity in focus letting the user know the Loading is done.
You can run it in a service and let the service inform your activities or your currently focused activity of certain events.
It's actually just a simple observer pattern.

Categories

Resources