Perform code when the Android application is closed - android

I'm making an application that detects incoming text messages.
When a message is received, the application must perform a certain action, depending on the sender and the content.
The problem is that the application must work at all times.
Detecting messages works through a BroadcastReceiver class. Via a Toast message I can see that the application (open or closed) receives the message.
The problem then is that I must perform certain actions, which are stored in a local database (DB40). But I can't access the database when the application is closed.
So, how can I perform database access and run other code (like making the phone vibrate, or play a ringtone) when the application is closed?
Thanks in advance

You can access the DataBase... because you are getting the context from the receiver, from the context create the instance of the Database and go further according to your requirement..
Check this...

You need to run a service to perform all the task even app is closed.
Just go through with few sample and study:
http://developer.android.com/guide/components/services.html

Do you know about Services?
You can use service as back-end to perform action on incoming messages or can use database.
Database need only 'Context' object for read/write and service provide 'Context' object by 'getApplicationContext()' method.
If you have any ambiguity about answer, just give me your little code, i will give you back with solution.

Related

Run code in background, Unity?

I would like to call a method every 10 seconds while app is closed. I have read that I have to create a service but I don't understand how. It would send information to a webserver (only personal uses).
Thanks guys!
If you want to send information to your webserver only while the application is running, you can use a thread within unity.
If you want some code to keep running, irrespective of whether or not your app is running, you need service. This should get you started on how to write a service.
You can write a small plugin in android native code, and include it's aar/jar in your unity project. Then you may send a message from your unity app via JNI to start the service. Totally depends on how you want to implement it though.
I have found a way of doing it. You can call a method via OnApplicationQuit() and this will run when the app gets closed. I have used this to get changes from my firebase database and send a notification when a child is added. It works. In this case you would have to subscribe to a method and this will run even if the app is closed. Then unsubscribe to this method via the OnAwake() method.
Check this
< https://developer.android.com/training/run-background-service/create-service>
You have to create a service but probably its very difficult for you.

How to send a notification in an android service?

In my android application, I have a server part containing a mysql database. I want to create a service whose role is to detect an update of a field. When the service detects a change in a table, it sends a notification to the user. I have not started writing the code but I have some questions to understand how the service works:
1) For access to the database I used AsyncTask, do I have to call this class in the onStartCommand method of the service ?
2) and for sending the notification, do I write its code in the onPostExecutemethod ?
3) Is the use of BroadCast mandatory ?
Thanks.
I would like to advise you to implement Firebase FCM notification to not consume the android resources for nothing.
good luck.
I found a solution by using BroadCast,Service and AlarmManager.

How to store flash information after call in android?

We get a flash message after a call finished.I need to take the details and store into sqlite db.
I need to get the duration and cost of that call from the flash message.
Then how to take this information.
You just use broadcast receivers to get the message. After that you have to parse the string to get the details like cost and time. But the main problem is different service providers have different format of flash messages. So you have to manage all these difficulties.
If I understand you want to be notified when some call is finished for storing the information in a sqlite database, right?
You can use a BroadCastReceiver that notifies you when the call state changes, here a brief explanation.
I don't sure if with the broadcast you get all the call information, if not you can always open the call log content provider example
Hope it helps :)

What is the best way to implement a "stub" on Android?

I'm creating an Android application that will register an Observer and listen for events, I will probably use one of the suggestions by Mark in this previous question.
However, my question is, how can I create a "stub" on Android that I can use to fire events at my applications observer? For the purpose of this example, we'll assume that my app is listening for a wifi signal strength, I need to create something that will run on the emulator (or device) that will mock up data to "pretend" it has a strong/weak signal, and to relay that to my app.
Any suggestions?
I am no Android expert but here is my catch.
If you are implementing an observable, I believe you need to create a Service by inheriting from ServiceBase.
Also create a content provider and allow other applications to insert data. The whole notification is built into the framework so if you have a cursor, you will get notifications of change in the data.
So here are the steps:
You run the service and register for notifications
Application is getting an instance of your service and registers to get back a token
They use your content provider to insert event along with the token they got
They call the notification
You will be notified whenever anything changes.
I know that there are notification services built into the framework but I have never had a chance to look into it.

first android service

I creating a small application that will basically use a background server to send data over HTTP. I dont know how to create services. Secondly there will be a couple of activities in my application. I want the activities to display a Context Menu when data becomes available. How can i do both. I have search for a while but the code i keep getting dose not seem to run on 1.6 api. How can i create the service and how can my activities listen to updates so that when a update is available they display a message.
NOTE: I do not need help on the HTTP part and the server part only creating the service and my activities listening to updates.
Kind Regards,
Give the Service docs a very good, thorough read.

Categories

Resources