I want to count the amount of unlocks, now there are post like this, but they don't explain how to efficiently count unlocks while the app is closed, can I get any code examples? I have tried to use background services, but I don't want a notification constantly displayed while the unlocks are counted.
Register a BroadcastReceiver. The filter you're looking for is ACTION_USER_PRESENT.
In your onReceive() function is triggered, you can load the current number from SharedPreferences, add one and save it back.
Related
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
I want this app to function like this
1.it will send the user location along with a message at interval of 2 mins to preselected contacts.
2.Location should be as presice as possible.
3.it should run in backround.
4.it stops only when the user stops it.
I tried many ways but it would work in foreground I want this to waork even when the app is closed if the user has to stop it he would have to open the app and deactivate it.
The IntentService class provides a straightforward structure for running an operation on a single background thread
See https://developer.android.com/training/run-background-service/create-service.html
My app is basically is a schedule app, where I'd like to show notifications x minutes before an (user-set) event occurs.
I read the documentation, but it only covers showing a notification at the time the notification code is being executed (which I guess is the only way).
My guess is that if I want to have notifications show up even when my app is closed, I need to somehow make the app run in the background and constantly check for upcoming events, calculate the time left, and show a notification when the time left equals the time before the event the user chose to be notified at.
I read this question (+answers): How to get android notifications when app was closed?.
There is a pretty detailed answer (using services) I could simply implement, but the other answer claims this approach is "crappy".
The second answer also suggest the usage of AlarmManager which, after reading the doc, doesn't sound that bad (executing code at a specific time).
However, since I'm targeting API19 it's "inexact" (according to the doc).
So what exactly is the most efficient (and the right) way to do this?
You need to implement alarm manager with service. Set the time user chose and show notification at that time. The sample shows a Toast, you can use notification/sound/dialog, anything.
Check out this sample.
When my phone receives a text message, it will upload it to my database. On the tablet I should get a notification about the new message as soon as possible.
How do I efficiently check for new messages from the database? I don't think a background process that queries the database every few seconds is efficient at all. It will drain the battery and it's huge waste of network.
Have a static BroadcastReceiver in your App as a listener. If a text message comes in the receiver will get started and onReceive() will get called. Now you can invoke a Service/ an Activity to save it to your database and put up a notification.
Here's a nice tutorial to get you started.
I want to create an application that can detect incoming calls and start my custom activity after a certain number of beeps (rings), I mean after 2 or 3 or 5 beeps (rings) my activity is triggered. How can I do it?
Thanks
I don't think you can count the number of rings the phone made since the start of the incoming call. There can't be a definitive measure of a single ring because the user can easily change the ringtone to a non-repetitive tune, for example, a song.
What you can do, however, is count the amount of time that passed since the arrival of the call. Set up a BroadcastReceiver for PHONE_STATE (you will need the corresponding permission in the manifest to receive the event). Once you receive the EXTRA_STATE_RINGING that came with the PHONE_STATE, set an alarm via the AlarmManager that will fire a Service that checks if EXTRA_STATE_OFFHOOK (broadcast when the call is picked up) has been broadcast after your waiting time. If not, then you can start your answering machine.
I have written a quick tutorial in my website on how to catch the call's arrival (when the phone rings), when the call is picked up, and when it ends.
My article about detecting incoming and outgoing calls, with the step-by-step instructions:
Detecting incoming and outgoing phone calls on Android
When you detect incoming call, you can start a timer, with interval equal to beepInterval * beepCount. And launch activity on this timer.