Two implementation schemes of android floating window

Hits: 0

The first implementation, independent of activity, uses the system-level WindowManager to add views:

<!-Add permission->
<uses-permission android: />
<uses-permission android: />

// Android 8.0 and above need to apply for permission 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !Settings.canDrawOverlays(BaseApplication.getInstance())){
   Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
   startActivity(intent);
   return;
}

WindowManager windowManager = (WindowManager) BaseApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
 // set width and height
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// set background transparent
layoutParams.format = PixelFormat.TRANSPARENT;
// Set the upper left corner of the screen as the starting point
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
// FLAG_LAYOUT_IN_SCREEN: place the window within the entire screen, ignoring other decorations (such as the status bar); FLAG_NOT_TOUCH_MODAL: do not block events from being passed to the following window
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
     // Set the form display type (TYPE_TOAST: a level with toast)
    layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
}
// Set the animation of the layout popup
layoutParams.windowAnimations = R.style.anim;
View contentView = LayoutInflater.from(BaseApplication.getInstance()).inflate(R.layout.content_view, null);
windowManager.addView(contentView, layoutParams);

The second implementation, to implement the floating window in the application, needs to rely on the activity, and the current top window can be obtained through the reflection mechanism:

/**
 * Get the top activity
 * @return
 */
private Activity getTopActivity() {
    try {
        Class activityThreadClass = Class.forName("android.app.ActivityThread");
        Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
        Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
        activitiesField.setAccessible(true);
        Map activities = (Map) activitiesField.get(activityThread);
        for (Object activityRecord : activities.values()) {
            Class activityRecordClass = activityRecord.getClass();
            Field pausedField = activityRecordClass.getDeclaredField("paused");
            pausedField.setAccessible(true);
            if (!pausedField.getBoolean(activityRecord)) {
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);
                return activity;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

// Add a floating window above the top window
Activity activity = getTopActivity();
View rootView = activity.getWindow().getDecorView().getRootView();
ViewGroup contentView = rootView.findViewById(android.R.id.content);
View floatView = LayoutInflater.from(BaseApplication.getInstance()).inflate(R.layout.float_view, null);
contentView.addView(floatView);
// Add animation 
floatView.startAnimation(AnimationUtils.loadAnimation(floatView.getContext(), R.anim.anim));

Leave a Reply

Your email address will not be published.