Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

android - Enable longClick in WebView

In the browser, you can longClick on URLs. In my WebView, you cannot. How can I make it so you can?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had this same problem.

Unfortunately, I could not find a way to make the standard browser menu options appear. You have to implement each one yourself. What I did was to register the WebView for context menus with activity.registerForContextMenu(webView). Then I subclassed the WebView and overrode this method:

@Override
protected void onCreateContextMenu(ContextMenu menu) {
    super.onCreateContextMenu(menu);

    HitTestResult result = getHitTestResult();

    MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                return true;
        }
    };

    if (result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // Menu options for an image.
        //set the header title to the image url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
        menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
    } else if (result.getType() == HitTestResult.ANCHOR_TYPE ||
            result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {
        // Menu options for a hyperlink.
        //set the header title to the link url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVELINK, 0, "Save Link").setOnMenuItemClickListener(handler);
        menu.add(0, ID_SHARELINK, 0, "Share Link").setOnMenuItemClickListener(handler);
    }
}

If you want to do something other than a context menu, then use an OnLongClickListener. However you want to intercept the long click event, the HitTestResult is the key. That's what will allow you to figure out what the user clicked on and do something with it.

I haven't actually implemented "Save Link" myself, I just included it as an example here. But to do so you would have to do all the processing yourself; you'd have to make an HTTP GET request, receive the response, and then store it somewhere on the user's SD card. There is no way that I know of to directly invoke the Browser app's download activity. Your "Save Link" code will look something like this:

HitTestResult result = getHitTestResult();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(result.getExtra());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
    URL url = new URL(result.getExtra());

    //Grabs the file part of the URL string
    String fileName = url.getFile();

    //Make sure we are grabbing just the filename
    int index = fileName.lastIndexOf("/");
    if(index >= 0)
            fileName = fileName.substring(index);

    //Create a temporary file
    File tempFile = new File(Environment.getExternalStorageDirectory(), fileName);
    if(!tempFile.exists())
            tempFile.createNewFile();

    InputStream instream = entity.getContent();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    //Read bytes into the buffer
    ByteArrayBuffer buffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bufferedInputStream.read()) != -1) {
            buffer.append((byte) current);
    }

    //Write the buffer to the file
    FileOutputStream stream = new FileOutputStream(tempFile);
    stream.write(buffer.toByteArray());
    stream.close();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...