Android webview Optimization Road _android

Source: Internet
Author: User

With the iteration of the app, the embedded HTML5 interface is getting more and more, and the problems caused by WebView this powerful component are increasing, for example:

    • 1, the oom problem caused by webview
    • 2, the Android version is different, using a different kernel, compatibility crash
    • 3, different versions of the implementation of different, and even the URI is not standard can cause different degrees of problems

In order to solve the above problems, we make the WebView module into an independent process

WebView Independent Process

Android allows an app to have multiple processes at the same time, allowing different modules to be processed in different processes as needed.

such as micro-letter v2. x+ version of the network part of the process of separation, separate into a separate process (: push), and the above two levels are still running in the micro-mail main process (: Workder). For WebView or other infrequently used features that have a memory leak problem, detach them into a separate tool process (: tools). Through the process of separation, the first reconstruction of micro-letters solves the dilemma that the system takes the initiative to kill the micro-trust service because of the consumption of micro-trust resources.

WebView the benefits of an independent process

Effectively increase the operation of the app, reduce the memory leakage caused by WebView to the main process memory footprint.
Avoid webview crash affecting the operation of the app master process.
Have control over the WebView independent process.

How WebView processes communicate with other processes

After the WebView independent process, it is found that the buried point function and the receiving main process data are not normal, and here the problem of interprocess communication is involved;

Process communication is nothing more than those kinds, aidl,messager,content provider, broadcast;

I'm not going to repeat it here, I'm doing it by radio.

WebView hardware acceleration causes page rendering to flicker

More than 4.0 of the system we turn on the hardware acceleration, webview rendering page faster, drag also more smooth. But one side effect is that when the WebView view is obscured by the whole and then suddenly recovers (such as using Slidemenu to slide the webview out of the side), the transition period appears white and the interface flashes. The solution to this problem is to temporarily shut down the hardware of the webview before the transition period, and then turn it on after the transition period, following the code:

if (Build.VERSION.SDK_INT >= build.version_codes. Honeycomb) {
Webview.setlayertype (view.layer_type_software, NULL);
}
configuration of WebView

Post my own configuration code below:

 WebSettings settings = Webview.getsettings (); Settings.setjavascriptenabled (TRUE);//Enable JS settings.setjavascriptcanopenwindowsautomatically (TRUE);
JS and Android interactive String cachedirpath = Pathcommondefines.webview_cache; Settings.setappcachepath (Cachedirpath); Sets the specified path Settings.setallowfileaccess (true) of the cache; Allow access to file settings.setappcacheenabled (true); Set H5 cache open, default shutdown Settings.setusewideviewport (TRUE),//Set WebView Adaptive screen size settings.setlayoutalgorithm ( WebSettings.LayoutAlgorithm.NARROW_COLUMNS);/set, if possible so that all columns do not exceed screen width settings.setloadwithoverviewmode (true); Sets the WebView adaptive screen size settings.setdomstorageenabled (TRUE);//setting can use Localstorage Settings.setsupportzoom (false); Turn off the zoom button Settings.setbuiltinzoomcontrols (false);//Close Zoom if (Build.VERSION.SDK_INT >= build.version_codes. Honeycomb) {webview.setlayertype (view.layer_type_software, null);} webview.setwebviewclient (New WebViewClient () {@ Override public boolean shouldoverrideurlloading (webview view, String URL) {view.loadurl (URL);@Override public void Onloadresource (webview view, String URL) {} @Override public void onpagefinished (WebView view, St
 Ring URL) {}});

HTML5 Jump Native Interface

There are many ways to jump the native interface of a Web page, such as a JS Java method, or a URI scheme, and you can do it by parsing the URL yourself.

Here, with compatibility in mind, the URL is blocked, and the scheme~ is customized in the manifest file.

Webview.setwebviewclient (New Webviewclient () {

  @Override public
  boolean shouldoverrideurlloading (WebView View, String URL) {
   parserurl (URL);//parse URL, if there is a URL rule with a jump native interface, jump native. return
   super.shouldoverrideurlloading (view, URL);

  @Override public
  void onpagefinished (webview view, String URL) {
   super.onpagefinished (view, URL);

  @Override public
  void Onloadresource (webview view, String URL) {
   super.onloadresource (view, URL);
  }
 );

In the manifest file, you can jump to the app page in your own browser via URI scheme, which acts as a distribution page for each page, parsing the data to determine which page to jump next:

<activity
 android:name= ". Ui.webview.CommWebviewActivity"
 android:configchanges= "orientation| Keyboardhidden|screensize "
 android:process=": WebView "
 android:screenorientation=" Portrait "
 Android : windowsoftinputmode= "Statehidden" >
 <intent-filter>
  <category android:name= " Android.intent.category.BROWSABLE "/>
  <category android:name=" Android.intent.category.DEFAULT "/>"

  <action android:name= "Android.intent.action.VIEW"/>

  <data android:host= "
   xxxx.com"
   Android:scheme= "kingp2p"/>
 </intent-filter>
</activity>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.