android瀏覽器開發小技巧集錦

本人和朋友們做了一段時間瀏覽器,將一些小技巧分享出來,先寫一部分,慢慢寫,同時也爲我們的瀏覽器打打廣告

我們的瀏覽器將要上線,名叫沙發瀏覽

1.網頁內的右鍵菜單

public boolean onLongClick(View view) {
		// 獲取點擊的元素
		HitTestResult mResult = mWebView.getHitTestResult();

		final int type = mResult.getType();
		switch (type) {
			case HitTestResult.ANCHOR_TYPE:
			case HitTestResult.SRC_ANCHOR_TYPE:
				//點擊的是鏈接
				break;

			case HitTestResult.IMAGE_TYPE:
			case HitTestResult.IMAGE_ANCHOR_TYPE:
			case HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
				//點擊的是圖片
				break;

			default:
				//點擊的是空白處
				break;
		}
		return true;
	}

根據是圖片還是鏈接還是空白做判斷


2.網頁內的自由複製

轉載請註明出處:http://blog.csdn.net/ethan_xue/article/details/7748075


/**
	 * 網頁裏 複製粘貼
	 * @param view webView
	 * @author ethan
	 */
	private void emulateShiftHeld(KeyEvent.Callback view)
	{
		try
		{
			KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
					KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
			shiftPressEvent.dispatch(view);
		} catch (Exception e)
		{
		}
	}

3.出錯界面

webkit自帶的出錯界面不夠霸氣,於是改爲自己做的出錯界面

new WebViewClient()
...此爲背景
@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			view.stopLoading();
			view.clearView();

			// 顯示出錯界面
			mWebView.loadUrl("file:///android_asset/error.html");
		}

4.點外部鏈接調用自己的瀏覽器

在manifest.xml裏主activity加入intent

<!-- For these schemes were not particular MIME type has been                  supplied, we are a good candidate. -->             <intent-filter>                 <action android:name="android.intent.action.VIEW" />                 <category android:name="android.intent.category.DEFAULT" />                 <category android:name="android.intent.category.BROWSABLE" />                 <data android:scheme="http" />                 <data android:scheme="https" />                 <data android:scheme="about" />                 <data android:scheme="javascript" />             </intent-filter>             <!--  For these schemes where any of these particular MIME types                   have been supplied, we are a good candidate. -->             <intent-filter>                 <action android:name="android.intent.action.VIEW" />                 <category android:name="android.intent.category.BROWSABLE" />                 <category android:name="android.intent.category.DEFAULT" />                 <data android:scheme="http" />                 <data android:scheme="https" />                 <data android:scheme="inline" />                 <data android:mimeType="text/html"/>                 <data android:mimeType="text/plain"/>                 <data android:mimeType="application/xhtml+xml"/>                 <data android:mimeType="application/vnd.wap.xhtml+xml"/>             </intent-filter>                 <action android:name="android.intent.action.VIEW" />                 <category android:name="android.intent.category.DEFAULT" />                 <category android:name="android.intent.category.BROWSABLE" />                 <data android:scheme="file" />             </intent-filter>

外部調用就ok了,連file文件都能調用,若自己調用的話

Uri uri = Uri.parse("file://data/data/test.html");
//   Uri uri = Uri.parse("http://m.baidu.com");     
  Intent it = new Intent(Intent.ACTION_VIEW, uri);        
  context.startActivity(it);     




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章