Android 手机浏览器的开发
最近看android资料,随手写了个简单手机浏览器应用,该应用很简单,主要包括AutoCompleteTextView 、WebView、Button控件,但是涉及到了很多android开发常识,例如:权限管理、布局标题栏状态栏隐藏、开辟线程监听事件、子线程不能更新主线程UI等,下面介绍一下代码示例:
1,修改AndroidManifest.xml文件,首先添加上网和应用旋转权限, 如下:
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.SET_ORIENTATION"/>
在此也可以添加如下代码使应用全屏,即隐藏状态栏和标题栏
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2, xml布局代码如下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <AutoCompleteTextView
- android:id="@+id/url"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/url"
- android:inputType="textUri"
- android:completionHint="@string/url"
- android:completionThreshold="1" />
- <WebView
- android:id="@+id/show"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_above="@+id/btnLayout"
- android:layout_below="@+id/url" />
- <LinearLayout
- android:id="@+id/btnLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/back"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="@string/back" />
- <Button
- android:id="@+id/forward"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="@string/forward" />
- <Button
- android:id="@+id/refresh"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="@string/refresh" />
- <Button
- android:id="@+id/home"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="@string/home" />
- </LinearLayout>
- </RelativeLayout>
布局设计中要注意实现适应屏幕。
3, java代码
- package com.example.webbrowser;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.Toast;
- public class WebBrowser extends Activity {
- AutoCompleteTextView url;
- WebView show;
- String[] booksArray = new String[]
- {
- "http://maps.google.com",
- "http://maps.baidu.com",
- "http://qq.com",
- "www.baidu.com",
- "www.163.com"
- };
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_web_browser);
- final Activity activity = this;
- show = (WebView)findViewById(R.id.show);
- show.getSettings().setJavaScriptEnabled(true);
- show.getSettings().setBuiltInZoomControls(true);
- //show.getSettings().setDisplayZoomControls(false);
- show.setWebViewClient(new WebViewClient()
- {
- public boolean shouldOverrideUrlLoading(WebView view, String strUrl)
- {
- view.loadUrl(strUrl);
- url.setText(strUrl);
- return false;
- }
- public void onPageStarted(WebView view, String strUrl, Bitmap favicon)
- {
- super.onPageStarted(view, strUrl, favicon);
- url.setText(strUrl);
- }
- public void onPageFinished(WebView view, String strUrl)
- {
- }
- public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
- {
- Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
- }
- });
- url = (AutoCompleteTextView)findViewById(R.id.url);
- ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, booksArray);
- url.setAdapter(aa);
- url.setOnKeyListener(new View.OnKeyListener()
- {
- public boolean onKey(View v, int keyCode, KeyEvent ev)
- {
- if (keyCode == KeyEvent.KEYCODE_ENTER)
- {
- String strUrl = url.getText().toString();
- Pattern p = Pattern.compile("http://([\\w-]+\\.)+[\\w-]+(/[\\w-\\./?%=]*)?");
- Matcher m = p.matcher(strUrl);
- if (!m.find())
- {
- strUrl = "http://" + strUrl;
- }
- show.loadUrl(strUrl);
- return true;
- }
- return false;
- }
- });
- // button
- final Button backBtn = (Button)findViewById(R.id.back);
- final Button forwardBtn = (Button)findViewById(R.id.forward);
- Button refreshBtn = (Button)findViewById(R.id.refresh);
- Button homeBtn = (Button)findViewById(R.id.home);
- backBtn.setEnabled(false);
- forwardBtn.setEnabled(false);
- backBtn.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- show.goBack();
- }
- });
- forwardBtn.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- // TODO
- show.goForward();
- }
- });
- refreshBtn.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- // TODO
- String strUrl = url.getText().toString();
- show.loadUrl(strUrl);
- }
- });
- homeBtn.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- // TODO
- show.loadUrl("http://maps.google.com");
- }
- });
- final Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- if (msg.what == 0x1111)
- {
- // whether can go back
- if (show.canGoBack())
- {
- backBtn.setEnabled(true);
- }
- else
- {
- backBtn.setEnabled(false);
- }
- // whether can go forward
- if (show.canGoForward())
- {
- forwardBtn.setEnabled(true);
- }
- else
- {
- forwardBtn.setEnabled(false);
- }
- }
- super.handleMessage(msg);
- }
- };
- // create thread to change button states
- new Timer().schedule(new TimerTask()
- {
- public void run()
- {
- Message msg = new Message();
- msg.what = 0x1111;
- handler.sendMessage(msg);
- }
- }, 0, 100);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // Check if the key event was the Back button and if there's history
- if ((keyCode == KeyEvent.KEYCODE_BACK) && show.canGoBack())
- {
- show.goBack();
- return true;
- }
- // If it wasn't the Back key or there's no web page history, bubble up to the default
- // system behavior (probably exit the activity)
- return super.onKeyDown(keyCode, event);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_web_browser, menu);
- return true;
- }
- }
除了AutoCompleteTextView输入URL和WebView显示网页内容外,应用底部有四个Button分别实现后退、前进、刷新、主页(暂未实现)等功能,到此就可以在android手机上使用自己的浏览器了。
4,应用截图: