My android app uses an SQLite database.
A service is triggered every weeks to update data. Once the service has downloaded the required data, it then writes data with writeData() into the database but I need the service to wait until the user is not using the app anymore.
So I have to call my writeData() function WHEN the app is put in background.
Is there a way to do that?
u can make a singelton class and whenever your app opens initialize this singelton
so every time before calling the write data check whether your singelton is null or not if null execute the writedata method if no then the user is opening the app
check this documentation about component callback http://developer.android.com/reference/android/content/ComponentCallbacks2.html#TRIM_MEMORY_UI_HIDDEN
That sounds like a use case for the onPause() life cycle callback.
Related
My app makes frequent use of the following pattern:
User clicks button
API request is fired
response is parsed
Callback returns data Data is updated in view.
However, due to the nature of the API these callbacks can take some time and I find that I can easily crash the app if I navigate around the app at a high pace. Mostly this is caused by NullPointerExceptions related to the fact that the activity and/or fragment no longer exists. My question is what the best practice is when dealing with these issues. Should I just check for null values everywhere? I've read somewhere that you should just avoid using callbacks to update the UI at all but I'm not sure what the alternative is.
Thank you all!
for Fragment you can check by isAdded()
public void onResponse(){
if(isAdded()){
// Do your stuff here
}
}
In activity isFinishing()
public void onResponse(){
if(!isFinishing()){
// Do your stuff here
}
}
Let me share how I did in one of my apps.
I created a class which extends Application and that class is responsible to initiate a database. It is Singleton Static database and everytime I need to do something, I call db.getInstance().doSomething()
When any API method is called, I start an AssyncTask which store the data on database after completed (in case of failure, nothing is saved).
When Database is updated, it sends a LocalBroadcast. You can send broadcasts to notify the error (which stops the Refresh animation and show a error message, for example).
Each activity has a BroadcastReceiver which register to receive the local broadcast sent by Database. I register during onStart() and de-register during onStop(). Each activity register to proper event (since you can create multiples intents and actions... This way, your activity receives only the desired intent and not every single broadcast of your app.
This way, when the activity is opened, it checks the data from database and if any content change, it receives a broadcast notification and take proper actions.
When the activity is closed, it no longer receive broadcast.. However, the updated data will be there on database after download is completed.
You must handle situations where some API call was already started to avoid calling twice (at least, until first call of same method finishes etc)..
You can also use ContentObserver to monitor some database etc.
This is one way to handle. It may work or not for you case etc... Just sharing since it may help you
i am currently making an android application. then i need to make a notification that only occur when there is certain event occur like insert or update.
what i want to try is how to use SQLITE trigger to make the notification occur on the android apps whenever user insert or update their data
thanks.
Take a look at the TRIGGER syntax
and you will discover there's no place to do it (unless you create your own function).
However, your application is the only one invoking insert() or update() so you know even before the trigger is invoked whether you want to send a notification. Then, do it where you invoke these methods.
I am doing an android app and I have an UI to show some data received from the server. The data is saved in the db in a controller.
When the app is started, this is what it is doing:
the controller instance is initialized on the Ui thread, it is singleton. The initialization is lightweighted. The UI will call the controller method to get the data saved in memory and show it.
having a worker thread to execute some controller method to read the data from db and save it in the cache in memory and notify UI after get it.
whenever there is some new data, the server will send a push to the client where an intentservice is started and the controller talks to the server to get the data and update the cache and after it completes, it notifys UI.
So the question is the 2 and 3, since both are running in different threads, so in order to make sure the db must be read and save in cache first, I have a flag in 3) so that before writing the new data in the memory, I always check the flag first. It will work but since I can foresee there will be more operations on the cache probably cross different threads and I really don't want to add the flag checking in all such places, so do we have any pattern/way to make sure the 2) always happens first?
sorry that I didn't find any similar post on it. thanks.
ok do one thing when your statement 2 is complete the execution at the last line of code call a broadcast receiver and inside onRecieve() method which is inside the BroadCastReceiver execute the statement 3.
I am trying to read data from the SQLiteDatabase when my application opens, and commit it all by inserting all of the changes when the Application closes. I know this isn't the most efficient, but the assignment I am working on is about UI mostly, not how the back-end works. How can I go about doing this? I tried creating my own Service class, but then I soon discovered that I can't seem to use the constructor for the Service. My goal is to use some kind of onCreate() and onDestroy() functions to work with the database when the App is started/ended.
In the situation like yours, the best approach is to use onPause() as described here:
Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity
I have an android app, ATV Trail Locator (http://www.youtube.com/watch?v=Wmk3cA2i2vI), that uses google maps.
Because my server is used for web serving and the Android app, I want to be able to kill the app in case it takes up too many server resources by setting a variable in my mysql database.
Because the application communicates with The server every second, it can get this variable and then shutdown the program if it is equal to 0.
I tried to call onDestroy() directly, but my program crashed. Do I need to use super.onDestroy() in order to kill the program without requiring back button push?
Thanks,
Have you tried calling finish() instead?
If this doesn't work, please post a code sample and your logcat errors so we can see what is happening.
Call finish() whenever you want to kill the current activity you're on. It'll call onDestroy() along with all the other required Activity lifecycle methods that need to be called.