Android WebView的使用
public class WebViewActivity extends Activity implements IWeibo { private WebView webView; private ProgressDialog progressDialog; private Handler handler; private static String url="http://api.t.sina.com.cn/oauth/authorize"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); //获取微博授权的网络路径 url=AuthUtil.getAuthorizationURL(); if(url.equals("") || url==null) { Toast.makeText(this, "授权地址为空", Toast.LENGTH_SHORT).show(); return; } //初始化 init(); //加载网络数据地址 LoadURL(url, webView); handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==1) { progressDialog.dismiss(); } } }; } @SuppressLint("SetJavaScriptEnabled") public void init() { if(progressDialog==null) { progressDialog=new ProgressDialog(this); } progressDialog.show(); webView=(WebView) this.findViewById(R.id.WebViewOAuth); //开启javascript脚本 webView.getSettings().setJavaScriptEnabled(true); //javascript接口用于读取授权码 webView.addJavascriptInterface(new JavascriptInterface(), "Android"); webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { LoadURL(url, view); return true; } //当页面加载完成以后获取该网页的源码文件中的授权码 public void onPageFinished(final WebView view, String url) { System.out.println("url >>>>>>>>>>>>>>>"+url); if(url.equals("http://api.t.sina.com.cn/oauth/authorize")) { view.loadUrl("javascript:window.Android.getPIN('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"); Intent intent=new Intent(WebViewActivity.this,AccessTokenActivity.class); startActivity(intent); } } }); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if(progress==100) { handler.sendEmptyMessage(1); } else { progressDialog.setMessage("已完成 "+progress+"%"); } } }); } /** * * @param url新浪微博授权地址 * @param webview浏览器这里是WebView控件 */ public void LoadURL(final String url,final WebView webview) { new Thread() { public void run() { if(url.equals("") || url==null ) { return; } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } webview.loadUrl(url); } }.start(); }