I'm having a specific problem that I'd love some insight on. Here is my code
Intent i = new Intent();
i.setDataAndType(media, mediaType);
startActivity(i);
This starts an audio activity for me and it even puts a notification item in the notification bar saying that it is an ongoing task. However, if I hit the Home button or Back button the ongoing task is immediately killed for me. I was confused by this behavior.
I found a way to keep the task going by the following: when the audio activity starts I drag down the notification bar and click the notification item ( which really just shows me the same activity again ) and when I do this it behaves accordingly. When I click the Home button it continues. When I hit the back button, it continues. For some reason when I click the notification item the "correct" Intent is fired.
What I'd like is for this behavior to start when I first launch the audio, because no one that uses my app is going to pull down the notification bar and click the notification item to get this to work properly.
Use a Service. Specifically, you will want to call startForegroundService() in the service's creation/start callback.
Related
I need to implement this feature but I am not sure if this is possible on android.
We have gcm listener service running. But let's say the app is open, and a notification arrives from the server. I need to have an activity triggered automatically without touching the notification status bar on the top of the phone screen.
In other words, without any user interaction once the notification arrives, if the app is running, an activity must be triggered immediately.
I took a look at this thread, but this is not really what I need.
Intent - if activity is running, bring it to front, else start a new one (from notification)
any clues or more info?
thx!
You can start an activity without another prior activity by using the FLAG_ACTIVITY_NEW_TASK flag.
Context c = getApplicationContext(); // or getContext(), or any other context you can find from the current app
Intent i = new Intent(c, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
The google play music app has a little 'X' in the top right corner, instead of displaying the time and a miniature notification icon in that location. Tapping the button kills the player and closes the notification. Great!
I'd like to do the same with my app - Is this only possible by inflating remote views and associating a custom button with an intent to destroy the service, or is there some notification builder methods that I'm overlooking which do that job?
There are no notification builder methods which achieve this. The only solution is to create a custom button with a pending intent to perform the action required.
I know this has been asked a million times in other forums, but I haven't got a perfect answer yet. This is my requirement:
When the activity goes to bacground, the timer starts and after 10 sec the notification appears. If the user doesn't click on the notification after 10 sec the notification changes.
When the user clicks on the first notification, the last viewed screen appears - i.e the activity comes to the foreground. This is achieved by:
Intent notificationIntent = new Intent(TimedAlert.this, FirstActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
But i want the timer to stop immediately after the activity comes to foreground. I.e in onResume () I give timer.cancel() but this doesn't seem to work. I also tried onNewIntent() but it works only when I am on the first activity when the app goes to background from the second activity. On coming to foreground the onNewIntent() method is not called, even though I have given single top for the second activity.
How can I achieve this behavior?
when the user click on the Notification, the system will call your notificationIntent, in your case the system will start FirstActivity, then in your FirstActivity you can stop the count down.
I have up to 4 separate alarms. When an alarm goes off, I display a status bar notification. If I have 2 alarms go off at the same time, I want to have only 1 status bar icon. If I cancel one of the two alarms, I still want to see that single status bar icon (since there is still an alarm going off, even though I cancelled the first alarm). When I cancel the last alarm on the screen I want to remove the status bar icon.
Is there a built in way to do this or do I have to keep track of what alarms are on the screen and only dismiss the notification if it's the last alarm?
Thanks for your help.
What I understood that you want only latest notification to be displayed and if you cancel your last alarm all should go. Here is my solution:
Call the same notification function every time you set notification as it will replace the previous one.
Pass an argument to notification function in case you want to cancel the notification.
If a notification is alredy there and now you want to cancel the next alarm and you want the previous notification should be there, then you should maintain a variable in sharedpreference which will tell you not to cancel the previous notification.You should check this before calling notification function
When you cancel the last alarm then you can pass the argument to notification function as you cancel any other alarm.
I want to set a onClickListner for a status bar Notification. How it is possible ? Please help. Now i can load a Activity by using the pending intent. I like to set a onClickListner for the notificatioin.
Regards
Parvathi
It is not possible to set an OnClickListener for a notification. Because of the way notifications are handled/displayed there is no way to guarantee that the process that created the notification will be running at the time the notification is clicked. This means that any code you wrote to provide click handling may not be running.
If you need click listener style behavior you will have to do it using the PendingIntent: set it to start a Service that runs the logic or to use an Intent that is received by a BroadcastReceiver. This will let you perform activity without requiring a UI.