currently, i'm starting a background service using Intent in my app, but is there anyway of determining when the service has completed?
..possibly by using some kind of listener, or sending a message from the service, to the activity??
First, activities and services can communicate: the easiest is to use a custom broadcast.
Second, if your service is short lived and is outlived by activity invoking it, then you might instead consider using AsyncTask to simply run your task in the background thread.
The usual trick you can use here is create a BroadcastReceiver in the Activity that listens for a specific Intent and then when the Service stops itself fire off that Intent.
You can send a broadcast Intent just before the service termination and implement a broadcast receiver.
Related
How do I get the actual Activity instance of the top current activity of a running Andoind application?
Reason: I receive a OnMessageReceived Data payload from Firebase while my application is running in the foreground, and I need to finish() it.
Note: there are tons of other info to get the ComponentName using getRunningTasks() or getAppTasks(), but all these does not seem to provide any way to get the actual Activity instance.
The better way would be to use a Local Broad Cast to inform your activity to finish itself.
You just need to register a Broad Cast Receiver inside your activity and then send the broadcast in your FCM Messaging Service. Check this page on how to do this.
Note: Make sure to unregister the receiver when you're done with the activity.
You can also use EventBus for the Same
I have the following problem. I am working on an android application that needs to do the following:
It should run on the background indefinitely (not possible, but close to indefinitely) and update a notification.
If the notifaction is clicked the background service should switch between an active and inactive state.
If the service is in the active state it should read sensor input and respond to it.
Now I tried the IntentService which I found out stops after a while. So now I am switching to the normal service. However what is the best way to respond to this notification click? Should the service extend the BroadcastReceiver to respond to a broadcast sent on the notification click or should I extend the SensorEventListener because it does need to listen to the sensor events? And if I extend the SensorEventListener how should I handle the notification click event?
When a service decides to notify about something it can create a notification in the status bar by means of NotificationManager.
When you tap the notification in the NotificatonManager you can start your Activity.
When this activity gets created you can bind from it to the server using ServerConnection so you can get and display any relevant data
Your service can "throw" a notification as described here: Notifitcations
For a notification, you can set an Activity which is opened when the user taps the notification or some control in the notification.
To communicate with a service, you can apply a "client-server-connection" between an activity and a service or two services by using Bound Services: Bound Services
According to a services lifecycle, you have to declare the states in the service when it is active or inactive. By interpreting the intent from the notification which is given to the activity, you can forward this over the binding (applied by a Messenger) and change your services's state from inactive to active or from active to inactive.
To sum up:
You define two states in your service (initial maybe "inactive").
When you start your application, bind the MainActivity and your background service so they can communicate (especially your Activity can tell your service something)
When you bind your service, this will also start its lifecycle where you directly can "throw" the notification with the "switch". Add your MainActivity to the notification as described in the API-Doc.
When returning from tapping the notification to your activity, define what the activity shall send to the service by using the Messenger. in your service, define a separate logic to switch the states
Throw a new notification to switch the state back
In your service, you can listen to the sensors by a SensorListener or anything else
I hope this helps you a bit - becase this is not an easy task it is a little bit extensive to post a Proof of Concept here.
What is the generally preferred method of using GCM in its current state?
The documentation only talks about using it with a BroadcastReceiver and only mentions Services in one sentence without further explanation.
In my application, I need to be able to react to an unknown number of successive GCM messages and queue them so I can process them one by one. This processing needs to be done in order the messages are received in.
This cannot be done with a BroadcastReceiver, as for every broadcast received, a new instance of my receiver class is created (this was the method I tried first as per the getting started guide). Can it be done with a service or, more precisely, is the instance of my service kept between messages received?
If so, when and how does this service need to be started, added to the manifest, etc.?
I do not need to interact with my main application/UI. The service can do its business on its own.
GCM message comes as broadcast so you must use BroadcastReceiver. If you need to queue them for any reason. just make your BroadcastReceiver hand the message to IntentService or anything else you find suitable for your task.
I'm facing a problem with the android broadcast receiver. I have a login page and after a successful login the app goes to another activity. And it contains a broadcast receiver. I am sending a broadcast once a separate service completes its action. The app works fine in a happy day scenario.
My problem here is, after i entered the username / password and hit the login button i send the app to the background. But when the app is in background that broadcast receiver wont receive the broadcast which i'm sending.
Anyone have a clue regarding this issue ?
Any help would be greatly appreciated.
Thanks in advance.
Are you unregistered the broadcast receiver in any of the activities onPause/OnStop/OnDestroy method? if yes then you will not receive the broadcast. make sure you have register the Broadcast receiver in the AndroidManifest. and not unregistered. if you wanted to receive intent in the background then you should not unregistered it.
Is you start separate service in another Activity right after you hit the login button ?
Because service is the entity that ment to run on the background.
If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, you send your application on the background. Now, at this point activity inserts to onPause. Therefore your second activity is not active and as a result can't call to the service.
I hoped it will you help to resolve your problem :)
Are you unregistered the broadcast receiver in any of the activities onPause/OnStop/OnDestroy method? if yes then you will not receive the broadcast. make sure you have register the Broadcast receiver in the AndroidManifest. and not unregistered. if you wanted to receive intent in the background then you should not unregistered it.
0
If you start separate service in another Activity right after you hit the login button ? Because service is the entity that ment to run on the background.
If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, the application will be in background. Now, at this point activity inserts to onPause(). Therefore your second activity is not active and as a result can't call to the service.
My application will launch a new activity (say calculator) using intents.
My requirement is: Is there any way to start the new activity in background (not in foreground)?
Can you suggest any solution?
thank you
Short version : Use threads.
Not-so-long version : read about threads.
What do you want to do exactly? Depending on that, you have several options:
You can use an AsyncTask
You can use a local Service
You can use a remote Service
You can use a Thread
Use BroadcastReceiver to receive the Intent and start Service or Thread do what you want to do.