Suspension window to achieve the traditional scheme
There must be many mature cases about the implementation of traditional suspension Windows and some old “black technology” suspension Windows. The implementation strategies are basically as follows:
- Type TYPE_SYSTEM_ALERT
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams()
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
Copy the code
Required permissions:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" ></uses>
Copy the code
- TYPE_TOAST/TYPE_PHONE type
7.1.1 The following does not require a permission statement and is hidden by default on meizu, Huawei, Xiaomi and other models. Users need to be guided to open the suspension window.
Problems with traditional solutions
The first scheme can not be adopted by many developers because of various limitations, so the more popular suspension window implementation method is the second.
However, we have our own principles:
- Can’t accept the models above 7.1.1, but the user still needs to actively grant permission to use the second method to realize the suspension window?
- It is not acceptable to hide by default on meizu, Huawei, Xiaomi and other models, and users need to be guided to open the suspension window, like this
function
- In-application display, no need to apply for any permissions
- In-app display, all models can display floating Windows by default, no need to guide the user to do more Settings
- Support drag and drop
- Move beyond the screen limit
- Automatically attaches to the edge of the screen
Use rules
1. Add to gralde dependencies
compile 'com. Imuxuan: floatingview: 1.0'
Copy the code
2. Add the following code to onStart and onStop in the base Activity class
@Override
protected void onStart(a) {
super.onStart();
FloatingView.get().attach(this);
}
@Override
protected void onStop(a) {
super.onStop();
FloatingView.get().detach(this);
}
Copy the code
3. Show suspension Windows
FloatingView.get().add();
Copy the code
4. Destroy suspension Windows
FloatingView.get().remove();
Copy the code
Attach Github link: github.com/leotyndale/…