start/stop android service based on call events - java

how can i start/stop a specific service based on the following:
user A call user B ( the trigger is user B answer the phone and service stopped when call is ended )
user B call user A ( the trigger is user A answer the phone and service stopped when call is ended )
i want to calculate the call duration of the call and store the result in database.
regards

The calculation of the call duration is already done for you. Use the CallLog content provider, please.
Even if for some strange reason using the official call log is not what you want, you do not need a service. Set up a manifest-registered BroadcastReceiver for ACTION_PHONE_STATE_CHANGED and store the start/stop times in the database. Do not have a Service hanging out in memory for the sole purpose of watching time tick by.
But, please, just use the CallLog, since the OS is doing this work for you, and you don't waste the user's battery doing duplicate work.

Related

schedule event run after 2 hour

My app is an Android app for booking tables.so the app directly communicating with the server.I need to call a specific event that is generated by the Android app.say an item is purchased by the Android app, so I need to hold that item for 2 hour so no one can use that item.i'm thinking change the flag inside the database to not available. After the use that is after 2 hour I need to release the item for the other users.
I know from trigger will done the job but is it possible to delete the crown trigger from the table and remove the scheduling.
Why would you hold/release like that? It doesn't seem robust... if there's some kind of communication error, the bike could get hung up permanently. I would run something based off timestamps and calculate the bike's availability in realtime based on the date. These calculations are going to be cheap to do.
If you want to handle this on the client side then you have to do it using AlarmManager, schedule an event to make an api call or just release it locally. But as u said cron job will be a better way to do it where u update the db after 2 hrs.
If you want to run particular Rest API
, after every 2 hr , You can either set Repeating ALARM , or you can user Timer and Timer Task
OR
If you want to just notify the user after 2 hr,
Then you can use cron-trigger with cloud messaging like FCM (firebase clod messaging )

service to check device state

I need to create an android service that:
Starts whenever the screen is on (whether it is at boot time or not)
sends a notification every 20 minutes (if the screen is on)
stops whenever the screen is off
Every tutorial I've read uses an activity, but I need this to be a service because the app is not supossed to be running other than when the user wants to change a setting. The documentation says I need an IntentService, but I cannot stop that manually and I cannot use a Service because it is a long running operation. I tried with an alarm manager but it didn't worked, I don't even bother to show you the code because I really don't understand it. I do not know how to make the service check if the screen is on or not, if I use a BroadcastReceiver it won't be inmediately processed so I am just stuck
To implement your requirement. You need 3 things such as Service, BroadcastReceiver & AlarmManager :
AlarmManager [which will fire after every 20 minutes]
Service [which will make changes like showing notification as system gets notifies after every 20 minutes for your particular msg]
BroadcastReceiver [which will check for screen on/off right from booting to shutting down]
Refer these links :
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115

Keep thread running after onDestroy

I am currently building an android application that will be used as an anti theft sort of application. Basically, once the alarm has bee launched, the application will monitor the accelerometer to detect movement. If it does, the user will have 15 seconds to enter a set password to deactivate the alarm, otherwise : BIIIIIP!
My problem is the following: how do I manage to keep the monitoring and counter process running after the activity is destroyed (if for example the user presses back) in a way that I can access it again from a notification.
I was thinking of using a thread to run the monitoring and counting process and when the notification was pressed, for example, the class could, in it's onCreate method, be aware whether an already existing thread is running and if so, get the handle to it?
Thanks.
What you are looking for is a Service. They are meant for this exact purpose; to run on the background (this does not mean a background Thread ) even if there are no Activities running.
You should consider using services for this purpose. Here is one of the example: http://blog.kozaxinan.com/2012/08/using-accelerometer-when-screen-off_16.html

Efficiency in a service

I got a service that generate the user location.
I got also a thread with the service instance that takes the location from the service and send it to the db.
The thread is sleeping for 1 min and the asking again for the address from the service and sending it to the db.
My question is should I create instead of this thread a service and the second service will be started every 1 min?
The advantage is that the service won't run when he doesn't needs to like the thread but the disadvantage is that I will have to create a new connection every 1 min.
What is better? Who is more efficient?
I'm usually from a line of thought that says that the best is always to not use Thread.sleep();
Said that, there're several options you can approach:
. Request location only every 1 minute, but that might have a weird battery impact as the GPS radio will be kept on during the non-working time.
. create a PendingIntent of your service and ask the AlarmManager to call it again in 1 min. This won't be keeping your GPS on all the time (if u program right), but it will need to fix the position again
. you also can in the same service (without destroying and re-starting it) use a ScheduledExecutorService to run the thread every 1 min. (GPS ON or OFF depend on programming)
Why do you need a thread or second service to insert the location updates inserted into the database. Why you cant you configure the locationupdates for every 1 minute in requestLocationUpdates method and onLocationChanged event, why cant you directly insert the records into the table. Running a second service or thread every minute might impact your battery performance. I have seen in one of my project.

Android app components - a word of advice needed

I am a complete newbie to Android development;
Basically, I am about to write an application, that will let the user to take photo, which (with a bunch of extra data) will be submitted to the remote webservice.
So I'm guessing I will need:
A Photo-taking application (Activity) that will gather all the extra data and put in the SQLite DB.
A background service looking up the DB in time intervals and sending the data over the Internet, optionally making web requests with current GPS location (I'm trying to keep in mind, that sometimes network would not be accessible).
A receiver object that will run the service at boot, and optionally check if the service needs to be restarted.
My concerns are:
Do I really need to monitor the service and care about anything bad that could kill it.
Will the battery last for at least 12 hours with a non-stop running service, making some networking/GPS actions in, let's say, 30-minute intervals. (G1/Dream)
What else should I be careful about?
Any ideas/suggestions will be appreciated.
a word of advice needed
Rutabaga.
Oh, wait. You're probably looking for something related to Android. OK, carry on.
A receiver object that will run the
service at boot, and optionally check
if the service needs to be restarted.
Yuck. Use AlarmManager and have your service behave more like a cron job/Windows scheduled task.
Do I really need to monitor the
service and care about anything bad
that could kill it.
Not if you use AlarmManager and have your service behave more like a cron job.
Will the battery last for at least 12
hours with a non-stop running service,
making some networking/GPS actions in,
let's say, 30-minute intervals.
(G1/Dream)
If you use AlarmManager and have your service behave more like a cron job, a 30-minute interval should be OK. Just make sure you shut down the GPS radio when you are done with it. Note that using the GPS radio from a cron job sort of task is a bit tricky, since it takes a while to get its first fix. You will also want to take a look at using PowerManager.WakeLock to keep the device awake until your work is completed.
What else should I be careful about?
Mynd you, moose bites kan be pretti nasti.
Beyond that and what I wrote above, you should be in OK shape. Note that what you are diving into is not exactly "newbie" material.

Categories