I am making an application which will check for files on storage directory and then utilize the files and upload to ssh server.
I was thinking about making a BroadcastReceiver which will start background Service on system boot, the Service will update AlarmManager which will start every hour/every day a class for utilize/upload file.
Can you give me advice if my logic is ok, or how can I improve it ?
Thanks.
This is why Google created the SyncAdapter.
You should look at the SyncAdapter example on the Android developers site.
Sync Adapters
There are a lot of moving parts; along with SyncAdapter you'll need to learn about Authenticator and ContentProvider (if you are dealing with the file system you can skip ContentProvider). This is the framework Google provides for asynchronously moving data to/from servers. You have the option to use a BroadcastReceiver if you want, but there are a couple other methods for scheduling data syncs.
Related
I'm trying to get a feature in my app that opens an ftp connection every night, compares the content of the ftp server to the contents of the sharedPreferences JSON and if there is a difference, upload the missing data to the ftp server.
So far I think I need to use an AlarmManager for this, so that the code runs even when the app is closed at night.
This is a completely new thing to me and I'm looking up some documentation and StackOverflow questions about it, but I can't really figure out if this is what I need to use for what I'm trying to do.
I found the following question which contains a nice example of AlarmManager. But can it also retrieve data from sharedPreferences? I was also wondering if I can run an async task (using coroutines).
EDIT: I read something about a workmanager. Would this be a better solution for what I'm trying to achieve?
Workmanager is introduced from Android 8. It is better then AlarmManager because it can manage the doze mode and more when you phone is in sleep mode. You can configure the workmanager to do the work each x mins with conditions (if the device connected, if the phone has % of battery ..)
The great issus with AlarmManager is that you don't garatute that it will be launched at the specific time because the system will manage all alarm with priority...
For more details : link
Yes, you can fetch data from shared preferences from your BroadcastReceiver. And you can also run asynchronous tasks from your BroadcastReceiver, such as coroutines or threads.
I am implementing a chat app in android. A vital part of this app is to sync with the server and local database. There are several methods to sync data between server and android device like AsyncTask, IntentService and SyncAdapter.
I prefer to use SyncAdapter, because it is more efficient and it handles most of the background tasks by itself.
When I read the developer page for SyncAdapter I found this,
Note: Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.
Does that means is it not good to use like chat app?
Also I need to mention a feature of SyncAdapter
Automated execution
Allows you to automate data transfer based on a variety of criteria, including data changes, elapsed time, or time of day. In addition, the system adds transfers that are unable to run to a queue, and runs them when possible.
So if it starts to sync when data changes (Since the new messages are stored in the sqlite database), I think SyncAdapter will be a good choice for Chat App.
Any Suggestions are appreciated.
Thanks.
Usually mobile app depends on backend implementation and app requirements, but generally you shouldn't use such methods for chat application, they won't give you up to date data.
I'd say when app is in background, you should use GCM for new messages notifications and when app is in foreground use something like RPC, xmpp, sockets or whatever that keeps your connection alive.
I have a website that sends and receives documents. I was thinking of building an Android app that notifies the user if a new document has been received, and displays document details if the notification is clicked. It doesn't have to be in real time, it could update in interval of five minutes or something.
What is the best way to update the Android app of changes in the website? I'm new to Android and I'm not quite sure where to start. I've heard of Services, BroadcastReceivers and Alarms, but I don't know if those are the right ideas.
Update: How do I update my Android db from my web db within an AsyncTask in my BroadcastReceiver? I'm worried I might have a "leak error" which sometimes comes up with my AsyncTask.
You can try to implement GCM or as the above-mentioned, work with an AlarmManager or the more efficient JobScheduler (requires API level 21!). Avoid doing heavy work on a BroadcastReceiver. Instead use the Broadcastreceiver to receive Alarms and start a Service in background. You may also have a look to WakefulBroadcastReceiver which holds a WakeLock for you. The Service could GET data from your webservice by using a REST architecture and update it's local database. Retrofit is a powerful open source library for a REST architecture. If there are new database records, you can inform the user by a Notification. Don't forget to check basic things like not starting the Service if the device hasn't got a network connection or to stop the Service after the work has been finished. I personally recommend to learn the basics first and then go to advanced topics. Good luck and pleasure.
I have an application that uses a webservice to get information and save the changes made by the user.
currently I have one service that runs while the application is open, and it has public methods for each operation. the trouble is that service is growing considerably and I'm thinking refactor, but the question is what is the best way?
I can think of the following options:
Deferred services the current service and that all are initialized at boot time application
Create small services and that these are initialized by local broadcast
although I have doubts about performance. Can give me some clue or example about which method is better, do not really care that changes are instantly synchronized, these are stored locally and can be synchronized when possible. Data sent are not many, so the synchronization is relatively fast
Synchronization processes are something like
Check if there is new data (I have several types of data, these are the ones that are growing)
Synchronize user preferences
Most likely there's no point of having Service running all the time. Instead, I'd go for IntentService. If possible, I'd also condifer using push notification (like GCM) so the server could let my app know that there's new data to fetch (or maybe even send it to me if you'd fit in the GCM payload limit).
I am in the process of making an application. I currently have a database and a method that uploads pictures (along with data corresponding with the picture/s, stored in the database) using a PHP script on the server. Neither the purpose nor the database's schema are important. I haven't really done anything like this before but have looked into it a bit. Basically the user will take a picture with the app. The picture and its data will upload if there is a network connection, but won't if there is no connection (duh). I want to have the pending images upload once a connection exists.
To do this...My current plans are to use a BroadcastReceiver to detect when the user is connected to a network then use a Service to upload the files. Is this the proper approach? How can I basically go about starting a service when connectivity exists if and only if there are pending uploads?
Your approach sounds reasonable to me in general.
You would register a broadcast receiver with your package, listening for android.net.conn.CONNECTIVITY_CHANGE.
If the activities also trigger your service, you may be able to properly handle every case with an IntentService alone, so you won't have to deal with the many questions around when to stop or start a service with a specific life cycle.
Hope this helps; you don't sound like a beginner so you'll be able to make ends meet.