Android service design considerations - java

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.

Related

How to get the Activity instance of an Android app running the foreground?

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

Alert dialog display even when the application is not running

I want to notify the user of my app when he receives special news (from my app).
How can i display some kind of a messeage to the user, even if my app is running in the background (or not running at all if possible).
I want him to be notified by text, and sound.
Thanks.
You need to write a service for doing this. Then in your service code use Notification class to show text and sound alerts.
Use Broadcast Receiver, to notify abt some action. Like updates, sms, or abt indicate when booting of a phone completes,etc... BroadCast Receiver works on the principle of Publisher and Subscriber pattern.
If your app is not running and you want to do something you'll need some kind of broadcast receiver to receive a trigger.
Probably what you'll end up doing is starting up at device boot, to schedule some stuff. This answer here should get you into the right direction
After that, the best way to notify the user is with notifications (or Toast messages if you're a great fan;))
Have a look at
Status Bar Notifications

How to run activity, if app in background?

I writing the network application and i have a problem with starting activity if the application in background. I want to start activity when some data comes to network. In my Actity A a have a receiver and when it receive some answer from server, it must run the Activity B. But if the App in background, Activity B not starting and metod onCreate() doesn`t execute. It execute only when the user go back to App. But its not really what i want, becouse in Activity B i need to start timer and i neen to enable GPS and some other work. Besides that, Activity B receive some data too, and if B not existing - this receiver will never receive anything.
I tryed IntentServise, but its not working - result the same as without him.
Any ideas? Thanks for any information :-)
Maybe your receiver is not getting intents when your app is background, for example if you're unregistering it in onDestroy method...
try declaring your broadcast receiver in manifest. In broadcast receiver class use starActivity to call act B. This works for me.
If you still have some problems maybe you should provide some source code(for example function where you're working with intents) to clarify your question.
You can't force activity to come back from background. You can use status bar to show notification for a user. Think also about it that this a mobile device, someone can in the middle of conversation or use GPS as navigation in car.

Android broadcast receiver when the app is in background

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.

determine when background service has completed

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.

Categories

Resources