I am sending data from one application to another by using aidl. The application which sends the data is in front and the application which receives the data shows a dialog box to the user is in background. Now problem is that the dialog box is not visible until i switch back to that application. Is their anyway i can show the dialog without switching back to other application. Like a battery low dialog from android which comes on top off everything.
3 Answers
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
// get the context from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = manager.getRunningTasks(1);
Context context = taskInfo.get(0).topActivity;
I'm not too sure if this actually works for activities that are not from your application, but I haven't got the resources to test it myself at the moment, before adding this as answer. It's worth a try if you actually are able to retrieve the current top activity with this. I just hope my memory gave me the right syntax...

- 382
- 1
- 5
You can achieve this using WindowManager
, the application which receives the data should have a background service
. If you are posting a broadcast event to pass data to other application, then when the application receives the data , start a service to display the dialog.
The dialog
can be a custom dialog , which need to be added as a view to WindowManager
Ok, Here is a sample with a custom dialog. You can try same approach with the default AlertDialog
When the application receive the event , start the service and In Service
when onStartCommand
called , show the dialog.
Follow this steps…..
Get the instance of WindowManager
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Inflate the custom view
View dialogView = View.inflate(getBaseContext(),R.layout.dialog_layout,null);
Add listener to the view.
dialogView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
windowManager.removeView(dialogView);
dialogView = null;
}
});
Set Window manager params
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER;
Add the view to Window Manager
windowManager.addView(dialogView, params);
Also add SYSTEM_ALERT_WINDOW
permission in manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

- 16,967
- 7
- 61
- 83
Actually, you need to add following permission to your manifest file to achieve what you want:
android.permission.SYSTEM_ALERT_WINDOW
http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW
Answer on following StackOverflow thread provides an example of how to do this:
Having application running above other app