Home > Java > javaTutorial > body text

Detailed explanation of multi-window solution for Android browser

高洛峰
Release: 2017-01-17 14:58:34
Original
1790 people have browsed it

Our Android platform is composed of one Activity after another. Each Activity consists of one or more Views.
So, when we want to display an interface, the first thing we think of is to create an Activity, and then all operations are implemented in the Activity, or it is a Dialog or Toast. This method is certainly simple, but in some cases, all we require is simple display, and using Activity is obviously redundant. At this time, how should we deal with it?

An Android application is also a Linux process at the bottom layer, but the concept of process is weakened at the upper layer and abstracts the interaction of Activity. The code directly controls the Activity, and the user's interaction is also the Activity.
Activity is an object abstracted from the perspective of user interaction, and is isolated from the process in concept and use. The process is similar to an adoption function. A process can have multiple activities. It can not only adopt the activity of its current application,
can also adopt the activities specified by other installation packages for the process. If the activity is destroyed, the process will not be destroyed (unless The system requires or code forces the process to be killed).

It turns out that the entire Android window mechanism is based on an interface called WindowManager. This interface can add views to the screen and
can also delete views from the screen. The object it faces is the screen on one end and the View on the other end, directly ignoring our previous Activity
or Dialog and the like. In fact, the underlying implementation of our Activity or Diolog is also through WindowManager. This
WindowManager is global, and the entire system is the only one. It is the bottom layer that displays the View.

Write a simple code:
Java code

WindowManager mWm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);    
Button view = new Button(this);    
view.setText("window manager test!");    
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();    
mWm.addView(view, mParams);
Copy after login

Generally when you first start developing android, you will make a mistake, that is, get getWidth() and getHeight in the constructor of View (),
When a view object is created, android does not know its size, so the results returned by getWidth() and getHeight() are 0,
The real size is calculated when the layout is calculated, so it will I found an interesting thing, that is, the reason why the length and width can be obtained in onDraw().


Use WindowManager to implement floating windows

       WindowManager.LayoutParams params;
        params = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,//TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP;
        manager.addView(tmpView, params);
Copy after login

You can add the View that needs to be added to the floating window to the window:

 if(view.getParent==null)//如果view没有被加入到某个父组件中,则加入WindowManager中
        wManager.addView(view,wmParams);
Copy after login

Among them, view is the view component that needs to be placed in the floating window.

If you want to remove it from WindowManager, you can execute the following statement:

        if(view.getParent()!=null)
        wManager.removeView(view);
Copy after login

In android, you can add multiple windows according to the above method. Problems caused by multiple windows:

2. Application life cycle issues
When other applications appear before the browser's main Activity, no matter how many browser sub-windows pop up in front, the browser's life cycle will enter the onPause state.

For more detailed explanations of multi-window solutions for android browsers, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template