如何在另一个活动中打开我的应用程序内的URL时将webview设置为默认查看器?

时间:2015-07-25 14:30:40

标签: java android

我有一个班级来展示webview:

像这样:

public class Tab2 extends Fragment {
    private WebView webView;

    @Override
    public View onCreateView(LayoutInflater inflater, 
                             @Nullable ViewGroup container, 
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_2, container, false);

        webView = (WebView) v.findViewById(R.id.webview2);
        webView.loadUrl("http://www.example.com");

我还有另一个课程,我把数据放在listview中。每个项目都可以在列表视图中单击。当我点击项目;使用Tab2类打开的URL。

我是这样做的:

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        String url = bookmarks.get(arg2).getURL();
        Uri myUri = Uri.parse(url );
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, myUri);
        startActivity(launchBrowser);
    }

});

有效。

我在我的应用程序中运行但是我遇到了问题。 我的应用程序(webview)并非侮辱!

例如,当我点击某个项目时,会打开一个Windows对话框,让我选择我的应用程序或默认的Android网络浏览器。

我不想要它。我想直接打开我的应用程序。我该怎么办?

1 个答案:

答案 0 :(得分:0)

以下是我打开文件的代码:

/**     
     * Open downloaded files
     *
     * @param context
     * @param webView  to display Html files (if null, use default browser or HTML Viewer)
     * @param filePath full local path
     * @param fileType mime_type
     */
    protected static void openFile(Context context, WebView webView, String filePath, String fileType) {
        if (fileType.equals("text/html") && (webView != null)) {
            webView.setVisibility(View.VISIBLE);
            webView.setInitialScale(1);           
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setDisplayZoomControls(true);
            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            webView.setScrollbarFadingEnabled(false);
            if (!filePath.startsWith("file://")) {
                filePath = "file://" + filePath;
            }
            webView.loadUrl(filePath);
        } else { // (pdf / word / excel files) or (html files but webView not available)
            try {
                File file = new File(filePath);
                Uri uri = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, fileType); // html files: "application/x-webarchive-xml": opened by default browser; "text/html": by HTML Viewer
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                ...
            } catch (Exception e) {
                ...
            }
        }
    }

无论如何,希望得到这个帮助!