Checking for updates from network continuously - how? - java

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.

Related

How to count unlocks in android while app closed?

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.

Android: How can I send an SMS in the background? (API Min is 21)

I'm creating an Android application that sends out SMS messages. I've got everything working except for one issue; It does not send out messages unless the app is open.
My code for sending is basic:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, messageToSend, null, null);
I want the message to be sent on a specific time. I have the time stored in a String called timeToSend and it sends perfectly if the app is in the foreground. How can I get it to send the message in the background? I've read up on alarmmanagers but I'm not sure if that is what I need.
Thank you.
Like #CommonsWare said, you need to handle your specific time logic. Why not use an AlarmManager which will call a Service in which your code for sending the sms actually "happens". If you do use an AlarmManager, please note that different versions of Android will (likely) be handled by different methods related to AlarmManager, possibly including code to reset the AlarmManager (if necessary). You also have to be aware of changes to how Services are managed in "the background" from Marshmallow on....
**EDIT: ** Please be aware that when rebooting your device you will have to reset your AlarmManager as well.
I think you could use AsyncTask, which runs in the background
The details on how to implement it are in this developer's page
AsyncTask enables proper and easy use of the UI thread. This class
allows you to perform background operations and publish results on the
UI thread without having to manipulate threads and/or handlers.

How to send sms at regular interval to preselected contacts?

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

How do I know when my service is called from a broadcast receiver vs Android?

So, I have a service for my app that is always running if the user turns it on. It listens with a broadcast receiver for USER_PRESENT to show a message. It is only supposed to show a message on unlocking.
The problem is though, when Android runs out of memory and kills it, then restarts it, it will show the message again, even if the user hasn't just unlocked their device. Is there a way to know who called the service?
I use service.START_NOT_STICKY, but would service.START_STICKY be better for this job? I guess I don't fully understand the differences but I'm pretty sure I want NOT_STICKY.
You can set the action field in the intent to a specific string when you call your receiver and then check it in onReceieve.
If it has your string then you called it , otherwise someone else.

Run BroadcastReceiver in the background and when device is alseep?

I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS. But I need it to run in the background and when the device is asleep. So would I use a Service that starts the BroadcastReceiver? If so, can someone give me some pseudo-code? And how would this work if the device is asleep?
I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS.
OK.
But I need it to run in the background and when the device is asleep.
Not really.
So would I use a Service that starts the BroadcastReceiver?
No. Your BroadcastReceiver should be in the manifest, so it can be invoked regardless of whether any of the rest of your code is running. That's why I say "not really" to "run in the background" -- you DO NOT WANT code running all the time in the background. Rather, you want to be able to receive broadcasts at any point, and that is what putting the receiver in the manifest is for.
If so, can someone give me some pseudo-code?
https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Monitor
And how would this work if the device is asleep?
It won't. However, an incoming SMS, like an incoming phone call, will wake up the device.

Categories