本人和朋友們做了一段時間瀏覽器,將一些小技巧分享出來,先寫一部分,慢慢寫,同時也為我們的瀏覽器打打廣告
我們的瀏覽器將要上線,名叫沙發瀏覽
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()...此為背景@Overridepublic 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);