Sunday, March 10, 2019

Android开发笔记-ch3.2.13 WebView, 3.2.14 Context


3.2.13 WebView

Refer to WebView reference, especially note this: When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. That is the reason in my code if I do this it will launch the default Web Browser App: mWebview.loadUrl("http://markham.bibliocommons.com") due to redirection. But if call setWebViewClient() or open the https url as mWebview.loadUrl("https://markham.bibliocommons.com"), it will open the page within the WebView. So better to use the WebViewClient as user may click links inside the webpage.
Refer to these links for about cookie: SO1, SO2, SO3. CookieSyncManager is deprecated in API21, use CookieManager flush() instead; Follow this if want to disable saving username/password in cookie. When set cookie, for API 15+, use *.*domain.com instead of domain.com. Somehow, calling getCookie in OnCreate will crash. Need to investigate.
Somehow, webview.loadUrl() with cookie doesn't work but webview.setCookie() works, make sure use the domain name for setCookie:
mWebview.loadUrl(getString(R.string.mpl_base_url), WebAccess.mCookies); //not working
mCookieManager.setCookie(getString(R.string.mpl_domain), WebAccess.mCookies.toString()); //working
mWebview.loadUrl(getString(R.string.mpl_base_url));
For supporting back key, refer to this SO. If there is only one webView, this code should be enough:
public void onBackPressed (){
    if (webview.isFocused() && webview.canGoBack()) webview.goBack();       
    else super.onBackPressed();}
If have multiple webView in the app, like in fragments, then should use this code:
webView.setOnKeyListener(new OnKeyListener()
{
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if(event.getAction() == KeyEvent.ACTION_DOWN)
        {
            WebView webView = (WebView) v;

            switch(keyCode)
            {
                case KeyEvent.KEYCODE_BACK:
                    if(webView.canGoBack())
                    {
                        webView.goBack();
                        return true;
                    }
                    break;
            }
        }
        return false;
    }
});
 

3.2.14 Context

Refer to this SO: its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application). You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).
Typical uses of context:
  • Creating New objects: Creating new views, adapters, listeners:
TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
  • Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE)   
getApplicationContext().getSharedPreferences(*name*, *mode*);
  • Accessing Components Implicitly: Regarding content providers, broadcasts, intent
getApplicationContext().getContentResolver().query(uri, ...);

0 Comments:

Post a Comment