Android hide navigation bar from service - android

I have a service in which, when the user shakes his/her phone, shows a full screen overlay window. I'm using this code to get it done:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mOverlayView, params);
It fills up the whole screen, EXCEPT for the navigation bar. Is there a way I can hide the navigation in my service that displays the overlay?
Note that my app is running in the background. The user may be running any other app in the foreground, and I want to show my full screen overlay window even if my main activity is not running.
All examples I could find online all seem to require an activity running...
Thanks for any help!

Related

Window over other apps/lock screen only with user permission in API26

I am trying to create an activity that would pop up in full screen and be shown over the screen lock (similar to every alarm clock in Android which shows a full screen dialog to dismiss an alarm). Here is my code to display the window:
if (Build.VERSION.SDK_INT>=26) windowType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
else windowType = WindowManager.LayoutParams.TYPE_TOAST;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
windowType,
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT
);
wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_alarm_screen, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
My concern here is that my app will need a special permission to show this window over other applications, and this scares users off. In API25 and lower I used TYPE_TOAST instead of TYPE_APPLICATION_OVERLAY, but in API26 it leads to:
FATAL EXCEPTION: main Process: com.app.alarm, PID: 31372
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.alarm/com.app.alarm.Activities.AlarmScreenActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W#601b8a2 -- permission denied for window type 2009
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
I saw this question, but it does not avoid asking a special permission.
Any other trick in API26 to avoid making a user allow a special permission that my app doesn't really need?

Finish app go back to last open app

I have an application that has a transparent background activity and with only a few buttons. When I switch to my application from another (using the button to switch between applications) I would like to keep the previous app visible in the background. Problem is that switching active app minimizes the previous one and is no longer visible.
Is it possible to do that? I realized that in the Play Store when I throw my request the background remains the Play Store.
Please get me out of my doubt if this is possible or not.
Reality: point 4 return home screen
expectation
does this solve your riddle?
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.activity_transperant, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);

Displaying activity(theme dialog) show above the Dialer in lockscreen?

Need to show the Activity (Theme dialog) over the dialer with some information populated in activity like true caller. In unlocked phone, it is working well.
But in locked phone, it didnt work as expected. Did some googling and found few flags needs to be added to make dialog appear in lock screen.
CallActivity.java
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_DIALOG | LayoutParams.TYPE_SYSTEM_ALERT |
LayoutParams.TYPE_SYSTEM_OVERLAY,
LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_NOT_FOCUSABLE |
LayoutParams.FLAG_TURN_SCREEN_ON |
LayoutParams.FLAG_SHOW_WHEN_LOCKED |
LayoutParams.FLAG_KEEP_SCREEN_ON |
LayoutParams.FLAG_DISMISS_KEYGUARD,
PixelFormat.TRANSLUCENT);
getWindow().setAttributes(layoutParams);
In the above code, i added changes related to make activity appear as dialog and few flags (FLAG_TURN_SCREEN_ON, FLAG_SHOW_WHEN_LOCKED, FLAG_KEEP_SCREEN_ON, FLAG_DISMISS_KEYGUARD) related to make dialog work well in lock screen (but it didnt).
I also starting the activity with (from broadcast receiver)
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
context.startActivity(intent);
add view on window manager with System Alert permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mParams.format = PixelFormat.TRANSLUCENT;
mParams.flags = 262184;
mWindowManager.addView(view, mParams);
Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().

Create an always-on-top alert on Android from a Service

I've been trying for weeks to find a proper solution to this problem, but I still couldn't find anything.
Basically, what I'm trying to do is to create an alert dialog box from a running service (that monitors for some event). The alert dialog should always appear on top of everything else, even if the phone is locked. I'm looking for something similar to what happens when someone calls you, or when an alarm clock goes off.
The best solution I've found was the following:
WindowManager mWindowManager = (WindowManager)
mContext.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.layout_alert, null);
mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.RGBA_8888);
mWindowManager.addView(mView, mLayoutParams);
And in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
The problem is that if I use the TYPE_SYSTEM_ALERT, then the dialog is always shown as I wanted, but I cannot receive any touch events on the buttons I have in the view. On the other hand, if I use TYPE_PRIORITY_PHONE, then I do receive all touch events - but the alert isn't shown above the lock screen.

how to avoid system overlay view being closed

My activity creates a system overlay view on the click of a button. I expect this view to remain visible over everything else till i close it on another button click in the activity.
The problem is Android automatically kills that view in a few seconds if i open couple of other applications. I know this is the expected behaviour of Android to manage it's resources. But is there a way to tell Android not to close this view? I tried creating the overlay in an IntentService, but it made no difference.
View creation code :
mFrameLayout = new FrameLayout(mContext);
mFrameLayout.setBackgroundColor(mContext.getResources().getColor(R.color.black));
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT
);
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(mFrameLayout, params);
Use service to create your system overlay view.
Not IntentService, just extend Service.
Make it work even if app goes to backgroung by returning "START_STICKY"

Categories

Resources