본문 바로가기
개발/Android

(Android) webviewClient

by 언제나초심. 2011. 4. 22.
반응형

package app.m;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class start extends Activity {

    WebView mWeb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        
        mWeb = (WebView)findViewById(R.id.webview);
        mWeb.setWebViewClient(new MyWebClient());
        
        WebSettings set = mWeb.getSettings();
        set.setJavaScriptEnabled(true);
        set.setBuiltInZoomControls(true);
        mWeb.loadUrl("http://주소");
       
        
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()){
            mWeb.goBack();
        } else if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            finish();
        }
        return true;
    }

        
        
    class MyWebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.matches(".*mp4.*")){
                Intent call_phone = new Intent(Intent.ACTION_VIEW , Uri.parse(url)) ;
                startActivity(call_phone); 
                return true;
            } else if(url.startsWith("http://")||url.startsWith("https://")){
                view.loadUrl(url);
                return true;
            } else {
                return false;
            }
        }
    }
}


반응형