Do a Task When Internet Connection is Available - java

I'm Working in an Android project that generates data and then it registers that connecting to a WebService, the data can be created even if the user doesn't have an active internet connection in that moment. So, I want a way to run a function that calls the webservice and pass the data when a valid internet connection is detected, all in background. I have read about SyncAdapter and JobScheduler, but I'm very confused about these options because I'm very new in Android development, any help or suggestion to decide which option is better to accomplish this would be very appreciated, thanks in after hand.

You should use broadcast receiver. By that way you can check internet connection in a broadcast receiver and then do some stuff in background.
this is a good tut to how use it.

Related

Android: have app send request to another app over network (on other phone)

I have two apps: "app" and "receiver", the latter of which also runs as a service on the phone. These are designed to be run on two separate phones - only one receiver will ever exist, which will be on my phone.
I'll try to make the problem as simple as I can: from the app, I'd like to be able to press a button saying 'hello', which would then load up an activity on the receiver's phone (from the service) also saying 'hello'. Pretty much I just don't know how to get the two devices to talk to each other. This will only be used over the same Wi-Fi connection. Would a broadcast be a suitable way to do this?
I've just started Android development a few days ago so I'm not sure how to do it - it's probably simple - and any help will be massively appreciated!
Many thanks!
Would a broadcast be a suitable way to do this?
No. Broadcasts can be used for inter-app communication, not inter-device communication.
Instead, devices on the same LAN can adress each other using their local IPs (usually 192.168.x.x).
One option would be to open a socket connection, see how to create Socket connection in Android?

Android HttpURLConnection connection refused after few minutes in service when ui is not visible

I am working on small chat application which is working fine when the app is visible to user. A service class which get and send data using post every 5 second when aap is visible to user and works fine. But when app.is closed and service works in background for few minutes fine. After few minutes i.e. apprx 3 minutes it get conmection refused error and never fetch data from server until.the app is again visible to user. I cant find any solution. please help.me
I uses HttpURLConnection for posting data, and a Thread and timer for regular posting.
Android suspend applications when they get into background, to make resources available for the foreground application. If you need to sync your data on the background you should use a Background Service or SyncAdapter.
Take a look at Best Practices for Background Jobs and Transferring Data Using Sync Adapters
Some tips:
Do not use manual timed HTTP requests for chats messages, its a very bad practice,
But, if you want to keep this, use some new http request library like "Volley" or "OkHttp" (this is my favorite),
If you want more professional and highly "best practice" stuff, use the Google's Firebase Cloud Messaging for chats apps, its use native google services for send messages to others apps, highly recommended.
Connection Refused can be your client (service) sending wrong data/values, wrong URL, wrong ports and wrong query, please post a piece of your code.

Accessing database from Broadcast receiver as well as with the app

I have built a simple application that accepts data entered by the user and saves it to the local sqlite database. If wifi connection is available it will transmit the data to a REST service hosted on a remote server.
I have done the above part and it is working pretty fine. If WIFI is not available it will just move on and will expect new data from the user.
When the wifi becomes available, i have registered a broadcast receiver which will hit my database and get the values stored and send them to the remote server.
I would like to know, while the broadcast receiver is trying to query my database, if the user is entering data at the same time and it is being saved in the same database, will it fire a SQLException.
As i recall, only one service can access the SQL instance at a time. If it will pose a problem what shall i do to overcome it. I have looked at ContentProviders, would that be the solution?
I am fairly new to android. Please advice.
You may want to take a look at this.
What are the best practices for SQLite on Android?
For me, I would suggest to always create a ContentProvider together with DatabaseHelper when you need Database, no matter you need to provide your data to external application or not. It is actually not difficult to do, the best reference I used to build my ContentProvider is DeskClock, the official app from Android.
Edit:
As a side note, you should consider to create a IntentService to be called by your boardcast receiver to do the work, as broadcast receiver should not be used for long running task, like sending things to server.
BroadcastReceiver#onReceive
When it runs on the main thread you should never perform long-running
operations in it (there is a timeout of 10 seconds that the system
allows before considering the receiver to be blocked and a candidate
to be killed).

sync data to server android

I'm new to android sync so I really need ideas to get started. Actually my app's requirement is like this: My app needs to connect to my database online. But my android app can be used offline meaning, the user can add records to a local db, say sqlite. And then when the user connects to the Internet, the app should automatically connect to the server and add the record the user added when he is not online. Can you give me some considerations and ideas on how to do this? I really need your help guys. Thanks.
I will recommend you to go for Sync Adapter. See these links
AbstractThreadedSyncAdapter and Creating Sync Adapter.
I have used them long time ago. I will say that it is the best approach for syncing data between device and server. Usually, email apps use these sync adapters.
Sample is also given there. Download it if you want to go deep inside of "HOW".
Thank You!
You need to run background service like android alarm manager. You need to check that device is connected via internet or not. if device is connected via internet then sync the data to server.
Check this like to the way of data sync
How to sync SQLite database on Android phone with MySQL database on server?
Thanks

Proper way to create and connect to a Socket in Android

I'm getting to a point in the development of an Android app where I've reached a stumbling block: how to create, manage, and connect to a Socket in Android.
My app needs to keep a persistent TCP connection to the server in order to exchange JSON formatted strings back and forth. After reading up on the subject, I've determined the best way forward is to create a Service when the app starts up (by extending the Application class and starting the Service in onCreate()), then read from/write to the Socket as needed. But how do I do that?
I obviously know how to create a Service and how to create and work with a Socket. But I don't know the best way to interact with one in an Android environment. Should I create an AsyncTask whenever I want to write data? Should I use Intents? Any help on the subject would be wonderful. And if my question isn't clear, I'll be more than happy to clarify anything.
Within the service, you can simply listen as you would in a regular Java application. This means you can safely wait for IO as you normally would.
You will have to use Intent when you wish to notify your activities about new data arrival using sendBroadcast and receiving it to your activities by either registering a BroadcastReceiver using registerReceiver or modifying your manifest file.
This is a good tutorial that may help you with broadcasting for Service <-> Activity communication.
Apparently I was thinking about this problem incorrectly. What I need is to implement a "Bound Service", which will give my Activity an interface to interact with in order to send and receive messages.
Here's a link to the tutorial I found which gives an excellent overview of how to do this.

Categories