android浏览器开发小技巧集锦

时间:2020-12-11 05:44:41

本人和朋友们做了一段时间浏览器,将一些小技巧分享出来,先写一部分,慢慢写,同时也为我们的浏览器打打广告

我们的浏览器将要上线,名叫沙发浏览

1.网页内的右键菜单

android浏览器开发小技巧集锦

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

android浏览器开发小技巧集锦

/**
* 网页里 复制粘贴
* @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);

android浏览器开发小技巧集锦