Call end listener in Android - android

I am developing an application in which I am storing call log details from CallLog provider.
Android stores call logs in a database and I am copying new rows from that database.
Now I want to copy call details when the user ends a call. For that I have used the PhoneStateListener with which I listen for state ringing, off hook and idle mode
but after the call is ended the idle method calls more than once, so I cannot use that.
Second I used ContentObserver for call log table and it gives me onChanged event when Android inserts call details in it and at that time I copy those details into my database.
Is it that the proper way to use ContentObserver?
Is there any other way to get details of new calls which is just ended?

That sounds like a valid use of the ContentObserver. A prior attempt at this is here: Intent to be fired when a call ends?.
I'd suggest you go with the idle method and simply track state in a database or variable or preference (so whenever the phone goes from OFFHOOK to IDLE you know the call ended, but when it goes from IDLE to IDLE then you do nothing).
Good luck.

Related

How to handle LifeCycle issues when using callbacks

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

how to prevent race conditions in my case

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.

Detect when user put the app in background

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.

How to know the status of call on android programatically

I have an app that has to perform certain actions after phone call is ended.
like in my application after every call i should save the call time, duration and write it to text file for further processing. Main problem here is how do I know that call is just ended.
I have checked Telephony manager like
TelephonyManager.CALL_STATE_RINGING
TelephonyManager.CALL_STATE_OFFHOOK
But I am still unable to achieve this task. Any help please.
Check out BroadcastReceiver. And here is a nice tutorial on how to catch changes in the phone state.

Fetch the updated call log and store it in a file

i'm developing an android app that retrieves the call log and store it in a text file. I'm using the ContentObserver to observe for the changes and fetch the updated entry once there is a change. I'm able to fetch the entries correctly if i get a miss call, incoming call and make an outgoing call by dialing the number from the phone book. If i make an outgoing call by clicking the number from the call log, the onChange() method is called thrice. The onChange() is called once i select the name from the call log, before making a call, such that retrieving the previous event once. Once the call is made, the outgoing event is retrieved twice, totally making three entries. Please do suggest me a solution.
Thanks!
In which URI did u register the observer. Is it android.provider.CallLog.Calls.CONTENT_URI? If so then the onChange() method will be called only once.
Also, relying in content providers is not a good idea. Refer http://android-developers.blogspot.com/2010/05/be-careful-with-content-providers.html. Instead of using content observers you can try using broadcast receiver with actions
android.intent.action.PHONE_STATE
android.intent.action.NEW_OUTGOING_CALL

Categories

Resources