0

I am looking for pseudo-code or real code.

My goal is to have a modal message box(as in "stay on top" and "not circumventable") shown at random times between two bounds, like t_in_minutes_minimum and t_in_minutes_maximum with always the same content.

If I do not tap okay, the messages should not pile up but close themselves after some time.

  1. Is there a way to do this with the alarm manager?
  2. How do I show a modal message outside my application?
  3. How do I make my message close itself after some time without tapping okay?

PS: I tried a lot.

Aurelius Schnitzler
  • 511
  • 2
  • 8
  • 18
  • The way to do this would be to have a service which runs a foreground notification with `.setOngoing(true);` and then either a handler timer or another `AlarmManager` to kill the service when needed. – Nick Friskel Jan 21 '17 at 07:03

2 Answers2

1

You can create alert window outside your application by using WindowManager api

What is WindowManager in android?

Code Snippet

WindowManager.LayoutParams p = new WindowManager.LayoutParams(
// Shrink the window to wrap the content rather than filling the screen 
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
// Display it on top of other application windows, but only for the current user
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
// Don't let it grab the input focus
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
// Make the underlying application window visible through any transparent parts
PixelFormat.TRANSLUCENT);
// Define the position of the window within the screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;

WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, p);

Add this to manifest

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

For Auto close message you can use handler to dimiss after delta of time.

Community
  • 1
  • 1
Rachit Solanki
  • 369
  • 6
  • 13
0

make a consistent alarm manager service class, and research on alert.dialogue activity. After displaying message through box check this link below

Execute function after 5 seconds in Android

and call finish(); function to close your message box.

Community
  • 1
  • 1