I am working on an android project that has an alarm clock like functionality.
I schedule an intentService for each alarm instance (fires at 9pm, for example.), this intentService builds the notification and displays it. The notification includes a fullScreenIntent, which works as expected and launches the activity. I use the following code to do this:
alarmActivityIntent = new Intent(this, AlarmActivity.class);
PendingIntent alarmActivityPendingIntent = PendingIntent.getActivity(this, alertSchedule.getIntentId(), alarmActivityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setFullScreenIntent(alarmActivityPendingIntent, true);
Notification mNotification = mBuilder.build();
mNotificationManager.notify(alertSchedule.getIntentId(), mNotification);
This works as expected when only one alarm is set to fire at a specific time, however if two alarms are set to fire at a specific time the behavior changes.
I want the first fullScreenIntent to start its activity, then when that activity finishes, show the next one. I believe I want to build up a task stack, and push these alarm intents onto it. However this is all new to me.
Is it possible to group these notifications?
If you are using the full screen Intent feature, you don't need to use an IntentService or a Notification at all. Just have the AlarmManager start your Activity when it fires.
You don't want to build a task stack, that is all too complicated. If you have multiple alarms that can fire at the same time, you can have the AlarmManager start the same Activity each time. Set the launch mode of this Activity to singleTop (in the manifest), so that if the Activity is already showing when the alarm fires, it will not create another instance of the Activity on top of the existing one, but instead will deliver the Intent by calling onNewIntent() on the existing instance of the Activity. In onNewIntent() you can save the data (extras) of the Intent in a queue that will be processed when the user finishes the currently shown Activity. To do that, just override onBackPressed() so that when the user presses the BACK button to finish the current Activity, you can check if there are any additional alarms waiting in the queue. If there are none, you can just call super.onBackPressed() to finish the Activity. If there is anything in the queue, you can remove the first element in the queue and display that in your Activity. Keep doing that until the queue is empty.
Related
I am trying to build an app that can be used for calling.
My CallActivity is declared singleTop in the manifest file. I have a foreground service (CallService) which is started as soon as the app goes to the background while the user is on a call, since the device must not sleep during a call.
The notification for my CallService allows the user to either resume the call or hangup. My goal is to have the user press a button on the notification and hangup the ongoing call without bringing the app to the foreground.
I have tried using PendingIntent.getActivity() to start the CallActivity once the app is in background, from the CallService. But I have not been able to hangup the call yet. Here is some code...
Intent returnToCallIntent = new Intent(this, CallActivity.class);
PendingIntent returnPendingIntent = PendingIntent.getActivity(this, 0, returnToCallIntent, 0);
Intent hangUpCallIntent = new Intent(this, CallActivity.class);
hangUpCallIntent.putExtra("ACTION_FINISH_ACTIVITY", true);
PendingIntent hangUpPendingIntent = PendingIntent.getActivity(this, 0, hangUpCallIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Right now both pending intents resolve to the same action which is hanging up the call while bring the app to the foreground. I figured out that this is happening because the 2 intents only differ in their extras and hence android does not distinguish them, i.e. intent#filterEquals() does not see any difference between them.
But the more important question is how can I finish() the CallActivity and have it pop off the backstack silently, without bringing it to the foreground. Also, after the CallActivity has been stopped, I need to stop the CallService in the background. So when the user taps the app in the recents screen, she/he should see the activity which was prior to the CallActivity on the backstack.
PS: Logic to hang up the call has been done in onNewIntent() method in CallActivity.
You can have your Activity register an anonymous BroadcastReceiver that listens for a specific broadcast Intent. When your Service wants to finish the Activity, it can just send the broadcast Intent that the Activity is listening for.
In onReceive() of the BroadcastReceiver, just call finish(). This won't bring the Activity to the foreground.
I want to show a notification while a timer is running and when the user clicks the notifiaction it opens the timer activity.
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Activity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
The problem right now is that when I start the timer and press home and then click the notification it opens the Activity with the running timer.
But when I start the timer, then open another activity (via ActionBar.NAVIGATION_MODE_LIST), then press home and click the notification it opens a new Activity (empty).
I thought addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); would help (thats what I used to navigate between Activities)
im using android:launchMode="singleTop" and android:configChanges="orientation|screenSize"
for every Activity.
I wouldn't rely on your activity persisting and it is unnecessary
Store the timer start time into sharedpreferences and then simply load up the start time when the activity is recreated.
If something is meant to happen after a certain time, like a countdown time, then you'll need to set up an Alarm and handle it that way.
Remember that activities are just UI. Don't trust an activity to be doing anything when the user isn't looking at it.
I have created a simple application having a button. Clicking it triggers a notification, and clicking on the notification launches a new instance of the same application. However, I wanted that clicking on the notification should bring me back to the application instance from which the notification was triggered. For this I consulted the Android docs for the FLAG_ACTIVITY_NEW_TASK flag-
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.
Based on this when creating the intent for passing to the PendingIntent, i set this flag. However, clicking on the notification still launches a new instance of the application.
What am I doing wrong ?
Remember that when you click the Notification it is from that Context that the intent is being launched. That context doesn't have the Activity on it's task (infact, it will be a blank task).
What this results in is two version of the same Activity (although still only one instance of you Application) running. Each Activity is running a different Task.
If you don't need duplicate Activities of the same type in any of your stacks you could use the answer here:
https://stackoverflow.com/a/2327027/726954
Otherwise, there are many ways to "fix" this problem, including singleton variables and Application Context methods that keeps track of which Activities are in a Running state.
You may need to search and refine your question for those.
A Task in Android is a separate User workflow. If you mange to see the Homescreen sometime, that usually means you start a new one.
Remove the flag and it should work. if it does not, try using Single top.
Try the below code:
Intent resultIntent = new Intent(context, YourActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN) resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, 0);
.setContentIntent(pendingIntent)
My android application starts a service to listen to headset buttons. While the service is running, I want to show a notification. Because it's also important it does not get killed, I decided to use the startForeground function in my service.
in the OnCreate of the service, I start BuildNotification():
public void BuildNotification() {
// Make sure the launch mode of the activity is singleTask, otherwise it will create a new one
Intent intent = new Intent(this, ListItemsActivityScroll.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
note = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.notification_text))
.setSmallIcon(R.drawable.vp_launcher)
.setContentText(getString(R.string.notification_content))
.setContentIntent(pIntent).build();
startForeground(1, note);
}
The first time the service is started, the notification gets displayed and stays in the status bar until the service is destroyed. However, if the service gets created for a second time, it shows only for a couple of seconds.
After it disappeared, the service is still running. I also executer 'adb shell dumpsys activity services', which does show the service to be running in the foreground and gives me also the correct flags set to the notitication:
isForeground=true foregroundId=1 foregroundNoti=Notification(contentView=com.example.mediabuttontest/0x10900a7 vibrate=null,sound=null,defaults=0x0,flags=0x62)
The 0x62 flags would mean the following are active: FLAG_FOREGROUND_SERVICE, FLAG_NO_CLEAR, FLAG_ONGOING_EVENT
Which I think is correct for keeping the notification active.
Does anyone understand this behaviour? Why it does work the first time the service is created, but not the second time? Is there any error in my code?
EDIT: Thanks for your time and comments, I've created another test application and started removing code until the problem disappeared. In the end, it was caused by enabling / disabling a broadcast receiver component:
pm.setComponentEnabledSetting(mRemoteControlResponder,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Somehow, this makes the notification disappear. In the document it also mentions about 'DONT_KILL_APP' can make your application behave unpredicatable:
Be careful when you set this since changing component states can make the containing application's behavior unpredictable.
I guess that's true :)
I have a service that shows a notification that I wish that will be able to go to a specific activity of my app each time the user presses on it. Usually it would be the last one that the user has shown, but not always.
If the activity was started before, it should return to it, and if not, it should open it inside of the app's task, adding it to the activities tasks.
In addition, on some cases according to the service's logic, I wish to change the notification's intent so that it will target a different activity.
How do i do that? Is it possible without creating a new notification and dismissing the previous one? Is it also possible without creating a new task or an instance of an activity?
No it wouldn't be possible to change the Activity once you have sent the notification.
You can start an Activity on your task stack that is not a problem, check out the notification service in the tutorial here:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
You have to set a pending intent on the notification:
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
// Set the info for the view that shows in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
You can see the pending intent takes a normal intent "new Intent(this, SecondActivity.class" so if you want specific behaviour (like bringing to the top instead of starting a new activity. Add the flags like you would normally to this intent. i.e. FLAG_ACTIVITY_REORDER_TO_FRONT (something like that)
Since platform version 11, you can build a notification using Notification.Builder. The v4 support library has an equivalent class NotificationCompat.Builder.
You can't change the Activity once you've sent the notification, but you can update the notification with a new Intent. When you create the PendingIntent, use the flag FLAG_CANCEL_CURRENT. When you send the new notification, use the ID of the existing notification when you call NotificationManager.notify().
Also, you should be careful how you start your app. The Status Bar Notifications guide tells you how to set up the back stack.