Listview update automatically - android

I am new in Android Development. I am fetching my database from the Xampp server and display data on listview it's working fine but when I update my database I need to refresh my activity every time. I want that I should not have to refresh it, again and again, it should automatically update the data in listview as a kind of real-time something! How can I achieve this? Please, someone, help me out... Thanks in advance!

Use notifyDataSetChanged() with the adapter instance that you have created for the List view.
This will notify any change in the data model fetched from the database.
Create a Service to listen to the port continously as per your need whenever there is a change in the data, you can send a signal to the application to fetch data again and then notifyDataSetChanged() will update the changes.
work of your refresh functionlity will be done by Service(not Activity).

Use push Notification concept
Android Push Notifications using Firebase Cloud Messaging FCM & PHP
when the server update send push notification from server and receive application by using services .
Inside activity make method to refresh listview call this method in the service.

I think Web socket and push notification both are a Right way to solve your problem. But as per my opinion, you should go with push notification it would be good because Socket is more costly to do it for that you need to make server strong. When traffic increased in the server then the number of socket connection would be increased and you need to manage that much of socket connections. FCM/GCM push notification is easy for that. You just need to send silent push whenever update data regarding particular user only.
For this best suit Firebase database if it is suitable for you in your current state.

Related

How to update app content when app is suspended in background

I have an Android app that has a service that handle Firebase messages. So far it's working for me and I receive messages properly even when app is in background. What I need is somehow update app content when a message is received. That mean use network to contact backend and download fairly long JSON data and put them in database.
I have model in repository which I use in Activity. Now I am considering whenever I have to somehow call activity and refresh data but I think activity is somewhat dead when app is in background? So I don't know if it is even possible.
I can download data inside the service but my understanding of service is that it shall be lightweight and all the model and network calls seems to be a bit overkill for the service. Am I wrong?
What is ideal solution considering standard android architectural approach? Thank you.

Poll data from remote database and create a notification to the user

Quite a while now, I have been using GCM to sending push messages to my android app users. Now I want to be able to poll data from my remote database to the app users when remote database receives new insertion. I want to create a notification when there is a new insertion into the remote database.
I understand I would have to create background services which is set to every second check from the remote database, this has set backs as the battery life of the user would be highly affected.
Again, I learnt about alarm manager which also sets to a period of time to poll data from the database.
Now I want to understand the better way to poll data from remote database and creating a notification to the user even when the app is inactive or in the background.
There is, you can make the server send a notification to the devices when new data has been added. This can be done with server code or if you don't want to do all the data base code there are applications that you can use. If you have your own server you can use parse.com since they are taking their hosting services down January 2017. If you don't have you can use Firebase and Batch to get the job done.

How to inform back Your order is ready? how to check data in server updated?

I am developing an application with login functionality. Users can login and make request for a thing through the server. When the order gets ready I need to inform him/her back (if he/she is not logged Out) that order is ready.
I am using server and an online database. How can I reply back to the person?? the device using may change.
You can use Google Cloud Messaging service to do that.
You can use a service to run in the background and in the service you can keep checking for the response from the server.
When you get a response, you can easily notify to the user.
Another way to this is with the use of AsyncTask. this is a kind of thread that keeps running in the background and updates accordingly on its postExecute method.
You can make this possible from a background service!
All you need to do is :
Start a background service that will check again and again after a specific time that the data is updated or not!
If the data is ready, send the data back to the user's device and check in the service that received data is not null and show a notification that your order is received.
A good example of Android Service

Communication between the same app in different smartphones

I would like to develop an application. It could be a game or whatever. I would have the same application in two or more devices. When one of them finish his tasks the other "client" must receive an notify that he has task to do and his datas should be updated automatically with the last changes. I guess that I would need a server in the middle where I'd save the model with the datas and send to them where the smartphones are communicating through it. It could be like a cardgame or kind of.
So,,,,
1. Two or more clients with the same application.
2. When one of them finish his task or turn, the other client should get a notify with his dates updates.
I have been looking at GCM, but I don't know if I could send complex datas through it or not,, and maybe there is a better way to make these kind of things.
Could someone give a clue where I can start??
Thank you!.
In your architecture, you must separate out the control and data aspects of the app.
You don't need the cloud to initiate a push of the entire data. If your app on any particular device gets a notification that an update is pending, then it can initiate the download at its convenience. Just use the GCM to push a notification that some task is pending for the app.

how to write a tigger in sql server to send a push notification (GCM) to android phone?

i have applied the demo code of GCM for server side on appache server and for client side on android device and it is working great; but i need to activate this service in my application as follows:
i have a database on SqlServer and need to automatically send a push notification to android phone whenever some certain data gets modified, i think i should use an after update and after insert trigger to do this, but i don't know how to do it.
any help will be appreciated, thanks in advance.
You should not do it from a trigger. Adding the latency of a GCM push to each table update will quickly bring the performance to unbearable lows. You have to decouple the trigger from the GCM call, and the best way to achieve it is via a queue.
You can use a table as a queue and have an external process monitor the queue and handle the GCM call.
You can use Service Broker and deliver the GCM call from an internal activated procedure or from an external process that monitors the queue
You can use MSMQ and monitor an NT queue from an external process.
My recommendation would be to go with first option as is the simplest and has the inherent robustness of simplicity. Read the linked article to understand what is required to use tables as queues, and do not cut corners.
All options still rely on a trigger to enqueue the notification, but it will be a local enqueue, not a GCM call.
I'm sure someone will think of the naive solution of invoking GCM from the trigger itself using SQLCLR: don't do it.

Categories

Resources