The use of agent web, the third party Library of Android

Time:2021-6-30
The use of agent web, the third party Library of Android

1: Preface
WebView is often used in Android development to load web pages. The performance and fluency of the system’s own WebView are average. Agent web is a third-party WebView, with better performance and more functions than the system’s own.
Agent web is a highly encapsulated Android WebView, simple and easy to use, with progress bar, supporting file upload and download, simplifying JavaScript communication, chain call, strengthening web security library. Let you easily integrate a lightweight browser in the application.
1. Function

  • Support progress bar and custom progress bar
  • Support file download
  • Support file download breakpoint continuation
  • Support download notification to prompt progress
  • Simplify JavaScript communication
  • Support Android 4.4 KitKat and other versions of file upload
  • Support injection of cookies
  • Strengthen Web Security
  • Support full screen video playback
  • Compatible with low version JS security communication
  • More power saving.
  • Support for wechat payment
  • Support to tune up Alipay (please refer to sample).
  • Default support positioning
  • Support incoming weblayout (drop-down rebound effect)
  • Support custom WebView
  • Support for jsbridge

Agent web is a lightweight and powerful web library, only 200K in size
The use of agent web, the third party Library of Android
2: Use
1. Dependence:

implementation 'com.just. agentweb:agentweb : 4.1.3 '// (required) use this basic WebView implementation
implementation 'com.just. agentweb:filechooser : 4.1.3 '// (optional)
implementation 'com.download. library:Downloader : 4.1.3 '// (optional)

2. Basic usage

public class FourActivity extends AppCompatActivity {
    private LinearLayout ll_root;
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_four);
 ll_root = findViewById(R.id.ll_root);
 Agentweb agentweb = agentweb. With (this) // pass in activity
 .setAgentWebParent(ll_ Root, new LinearLayout. Layoutparams (- 1, - 1)) // pass in the parent control of agentweb. If the parent control is relativelayout, then the second parameter needs to pass in relativelayout. Layoutparams. Usedefaultindicator() // use the default progress bar
 .createAgentWeb()
 . ready() // try again
 .go(" https://www.baidu.com ");// If you use HTTP, you need to configure it
 }
}

//Add permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

JavaScript calls Java, and some JS methods are created in the androidinterface class

//JavaScript calls Java
agentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(agentWeb,this));

In the androidinterface class

public class AndroidInterface {
    private AgentWeb agent;
 private Activity context;
 public AndroidInterface(AgentWeb agent, Activity context) {
        this.agent = agent;
 this.context = context;
 }
 
 @JavascriptInterface
public void callAndroid(String url) {
//To mark with @ JavaScript interface
}
}

Calling JavaScript methods

//Calling the method callandroid in JavaScript
mAgentWeb.getJsAccessEntrace().quickCallJs("callAndroid");

event processing

//Event handling of mobile phone
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (agentWeb.handleKeyEvent(keyCode,event)){
        return true;
 }
    return super.onKeyDown(keyCode, event);
}

Click the back button

@Override
public void onBackPressed() {
    if (agentWeb!=null&&agentWeb.getIEventHandler().back()){
        Log.i("callBack", "go back");
 }else {
        Log.i("callBack", "finish");
 finish();
 }
}

Follow the life cycle of activity or fragment to release CPU and save power

@Override
protected void onPause() {
    mAgentWeb.getWebLifeCycle().onPause();
 super.onPause();
}
@Override
protected void onResume() {
    mAgentWeb.getWebLifeCycle().onResume();
 super.onResume();
}

Create a WebView client to help WebView handle various notifications and request events
There are the following methods
onLoadResource()
onPageStart()
onPageFinish()
onReceiveError()
onReceivedHttpAuthRequest()

WebViewClient webViewClient=new WebViewClient(){
    @Override
 public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
 String title=view.getTitle();// Get the title of WebView
 if (!TextUtils.isEmpty(title)){
            setToolBarTitle(title);
 }
    }
    @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
 }
    @Override
 public void onPageCommitVisible(WebView view, String url) {
        super.onPageCommitVisible(view, url);
 }
    @Override
 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
 }
};

Create webchrome client, assist WebView to process JavaScript dialog box, website icon, website title, loading progress, etc,
There are the following methods
Onclosewindow (close WebView)
onCreateWindow()
Onjsalert (alert on WebView can’t pop up, you need to customize your webchromeclient to handle Pop-Up)
onJsPrompt()
onJsConfirm()
onProgressChanged()
onReceivedIcon()
onReceivedTitle()

private WebChromeClient webChromeClient=new WebChromeClient(){
    @Override
 public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
 }
};
//Use is added to the chain call

Agentweb = agentweb. With (this) // pass in activity
.setAgentWebParent(ll_ Root, new LinearLayout. Layoutparams (- 1, - 1)) // pass in the parent control of agentweb. If the parent control is relativelayout, then the second parameter needs to pass in relativelayout. Layoutparams. Usedefaultindicator() // use the default progress bar
. setwebviewclient (webviewclient) // join webviewclient
. setwebchromeclient (webchromeclient) // join webchromeclient
.createAgentWeb()
. ready() // try again
.go("https://www.baidu.com");

Modify the default background color of agentweb

FrameLayout frameLayout =agentWeb.getWebCreator().getWebParentLayout();
frameLayout.setBackgroundColor(Color.BLUE);

3: Note:

  • Alipay needs to introduce Alipay SDK and relies on it. WeChat payment does not need to do anything.
  • Agent Web uses theAlertDialogNeed to rely onAppCompatTheme.
  • setAgentWebParentI won’t support itConstraintLayout
  • mAgentWeb.getWebLifeCycle().onPause();Will suspend all applicationsWebView
  • minSdkVersionLess than or equal to 16WebViewPlease note thatJSThe communication between them is safe.
  • The package name has been updated in the version of agent web V3.0.0 or above. For confused friends, please update your confused configuration.
  • Multi process failed to cancel download,Solution

Specific GitHub address:[https://github.com/Justson/Ag…

End: unity of knowledge and action