Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE
Ref : https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
TYPE_PHONE
public static final int TYPE_APPLICATION_OVERLAY
This constant was deprecated in API level 26.
for non-system apps. Use TYPE_APPLICATION_OVERLAY instead.
Window type: phone. These are non-application windows providing user interaction with the phone
(in particular incoming calls). These windows are normally placed above all applications, but behind the status bar. In multiuser systems shows on all users' windows.
TYPE_APPLICATION_OVERLAY
public static final int TYPE_APPLICATION_OVERLAY
Window type: Application overlay windows are displayed above all activity windows (types between FIRST_APPLICATION_WINDOW
and LAST_APPLICATION_WINDOW
) but below critical system windows like the status bar or IME.
The system may change the position, size, or visibility of these windows at anytime to reduce visual clutter to the user and also manage resources.
Requires Manifest.permission.SYSTEM_ALERT_WINDOW
permission.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
The system will adjust the importance of processes with this window type to reduce the chance of the low-memory-killer killing them.
In multi-user systems shows only on the owning user's screen.
we have to use TYPE APPLICATION OVERLAY on device Oreo or later we have already example as Manoj Perumarath gived
you need to defined windowlayout params like this
//if device is Oreo or latter if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//or else
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
Manifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
//...other stuff
<service
android:name=".serviceClass"
android:enabled="true"
android:exported="false"/>
MainActivity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, APP_PERMISSION_REQUEST);
}
else
{
//start service
}
Read once
- https://www.spaceotechnologies.com/android-floating-widget-tutorial/
- https://www.journaldev.com/14673/android-floating-widget