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
Related
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.
Hi I am starting to look into android development and I couldn't find a good tutorial on snooze function. Here goes my question:
I have a simple alarm clock that I'd like to implement snooze. I have a AlarmActivity pass an intent to start AlarmAlertActivity. However, I when the snooze button is pressed, I want to call finish on AlarmAlertActivity. I have the snooze method written in AlarmAlertActivity using timer. However, when the AlarmAlertActivity class is finished, the timer no longer runs. I don't really want to do another intent to go back to AlarmActivity, because there might be multiple snoozes. Any help is appreciated!
You want to implement this functionality as a Service, not as part of an Activity. From the docs:
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
You can design your AlarmAlertActivity to interact with the service (starting it if necessary). The service can then broadcast a message when the snooze goes off.
It's hard to provide any specific advice because you have not provided any details about what you're doing. However, the code on this thread might provide some guidance.
hello everyone and thank you in advance. My problem is that I have an app that the user will start in the foreground and will set a variable as a timer. The user will most likely close that app, or the phone will go to sleep. So I need the app to background and then run code to send a notification when that timer hits 0.
For the timer I would use unix time and have the background process check to see if it matched or the current time surpassed when the timer goes off.
I would like to know - A) how to send the app to the background with these instructions
B) how to then foreground the app, or at least be able to send a notification after running some code
Again, thank you very much, let me know if I was not clear or if I missed someone's previous thread.
This post should be a good place to start, it looks like what you want to use is a service.
How to run an android app in background?
Look at extending the Android Handler class to create a custom Handler that creates a notification when the Handler receives a Message that you send it using the method sendMessageAtTime(android.os.Message, long uptimeMillis) or sendMessageDelayed(android.os.Message, long delayMillis). When that timer went off could be specified using the long parameter of either of those methods.
Hope this helped!
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.
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.