How to bring activity to front from notification in drag down menu? - android

I am working at an application that uses a notification(with a foreground service).
The use case is the following:
If the activity of the application is killed, when clicking on the notification's icon the activity is created and brought to foreground.
If the activity of the application is not visible, when clicking on the notification's icon the activity is simply brought to foreground.
For re-creating/bringing the activity on foreground I am using the following broadcast receiver in my foreground service:
private static class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent intent = new Intent(context,MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction("android.intent.action.MAIN")
.addCategory("android.intent.category.LAUNCHER");
context.startActivity(sIntent);
}
}
The desired action is pretty much similar to the default music player on an android phone, i.e: when clicking on the icon it's activity is simply brought to foreground(or re-created and brought to foreground in case it was previously killed)
This a screenshot from a Samsung S4
first notification is from usb - we do not care about it
second notification is my application - when clicking on the icon the activity gets created but not brought to foreground(the user has to swipe back the menu to see the activity)
How can I bring it to front without the additional swipe-back-menu? (in the similar way how music player app does?
PS: Minimum Android SDK version is 16

As far as i know, theres no possibility to bring your activity in front of the notification menu. But you're able to close the menu automatically with:
Intent i = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(i);

Related

Start pending intent instead of main activity, on app icon click

I have a foreground service on my application. When service running, notification will be display with a pending intent(this pending intent is not my main activity). When click on the notification, pending intent will be starts. Its working fine.
Following are my activities
Main activity - LoginActivity.java
Pending intent activity(which displays when click on notification) - HomeActivity.Java
When click on app icon while service is running, I need to launch pending intent activity(HomeActivity.java) instead of main activity(LoginActivity.java)
How could I do that?
You cannot change what the launcher icon points to dynamically at runtime in a reliable fashion.
You are welcome to have it point to an activity set up with Theme.NoDisplay, which then determines what actual activity should display, starts that activity using startActivity(), and then calls finish() to get rid of itself.
Or, have the launcher icon always point to HomeActivity, which has the logic to detect that a login is needed and then starts LoginActivity.

How can I achieve the following start activity intent mode

I have a broadcast receiver launching a transparent activity while my application is either not launched at all or is in the background. This transparent activity behaves like a pop-up-box that appears on top of the android OS (thus transparent).
If my application is not launched at the moment (exists neither in foreground nor background), when the broadcast receiver triggers the start intent action everything behaves as it should and my transparent activity appears on top of the android OS.
I have another scenario where my application is paused - is in the background. From this state, if my broadcast receiver is triggering the action to start the transparent activity it does so in a way that it moves my last activity on the screen and my 'transparent activity' on top of it thus not being transparent anymore, I can see through it the contents of the activity behind it (from my application). This behavior is not desired and I wan't to change it in a way that when the pop up activity is started, the activity that was before be invisible. How can I do this, how can I hide all other activities from my app in the stack and have only the last one visible !?
This is how I send the intent:
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
use this
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Android Notification Handling Suggestions

So I have a basic notification application. Here's the current configuration details:
GCMIntentService
NotificationActivity
:
---->ViewMessageActivity
:
---->ViewUserDetailsActivity
GCMIntentService is configured to do the following: Upon receiving a notification, a pending intent with PendingIntent.FLAG_ONE_SHOT is created and the intent adds the Intent.FLAG_ACTIVITY_NEW_TASK flag.
Currently, I have my NotificationActivity defined in my manifest as android:launchMode="singleInstance". If I set it to android:launchMode="singleTop", when the user clicks the notification a few things can happen depending on Android Version (or so my two devices show)
In Android < 2.3, If I'm in another activity (say ViewMessageActivity) and I press the notification from the shade, nothing happens (I don't receive anything from onNewIntent)
In Android 4.2 (Not sure about other versions), If I'm in another activity (say ViewMessageActivity) and I press the notification from the shade, nothing happens (I don't receive anything from onNewIntent) AND the notification disappears from the shade
If I'm in NotificationActivity everything works great regardless of version
So this led me to set it to singleInstance instead of singleTop. Now when the user presses the notification and the user is in a different activity (say ViewMessageActivity), the notifications intent is handled appropriately HOWEVER, my NotificationActivity tries to create a new instance of ViewMessageActivity for the message clicked in the notification shade and it does not create a new activity. Instead, shows my already opened instance. I've tried adding flags such as Intent.FLAG_ACTIVITY_NEW_TASK but neither my onCreate or onNewIntent methods are called in my ViewMessageActivity.
Ideally, this is what I would like to happen: When the user has navigated to another activity (say ViewMessageActivity) from the main activity (NotificationActivity) and the user presses a notification from the notification shade, I would like the NotificationActivity to be flagged to update its ListView view onNewIntent which will create another ViewMessageActivity instance. Note: I have no launchMode option set for any other activity.
Any suggestions?
Vince

How to resume an action when quitting with home button?

I'm developping a SIP application. I have a little problem: when "reducing" the application with home button and i make a call to the phone, i have the coded ringing incoming call but the application doesn't shows. How to pops up all the application UI when having an incoming call ?
Thank you for your help.
EDIT:
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Dialog dialog = new Dialog(context,intent);
dialog.répondre();
}
}
So, you have an Activity that is stopped and you want to pop it up when call arrives. Approach I would suggest:
originally start that Activity with flag FLAG_ACTIVITY_SINGLE_TOP
override function onNewIntent() in that Activity and process incoming Intent depending on action code from Intent (you define them to distinguish reasons for popping up)
when you want to move that Activity to foreground again, call startActivity() with some action code (you can to that from Service as well). If Activity is not started, it will be. If it is started, it will not be re-started but resumed and you will receive your Intent in onNewIntent() and your Activity will be moved to foreground.
UPDATE:
onNewIntent() handling example:
\android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\app\SearchQueryResults.java

Activity not being reused

I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.

Categories

Resources