如何打开市场://链接下面的代码

时间:2014-11-20 16:15:01

标签: android eclipse android-fragments

以下代码用于在我的Android应用上打开网页。我想在webview上打开市场://链接。我尝试了一些在这里讨论的例子。但没跟我合作。 如何允许以market://开头的所有链接在点按时自动打开Goog​​le Play商店?

package my.webview.test.com.fragment;
import radioklub.sekhontech.com.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.webkit.DownloadListener;


public class WebFragment extends Fragment {
//Constant
private static final String TAG = "WebFragment";
// Member variables
private String mUrl;
private boolean mIsLoadFinish = false;

// View Member Variables
private WebView mWebView;
private WebViewClient mWebViewClient;
private Button mBtnBack;
private Button mBtnForward;
private Button mBtnRefresh;

@
Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@
Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_web, null);

    // Init WebView
    initWebView(view);

    return view;
}

@
Override
public void onResume() {
    super.onResume();
    mWebView.loadUrl(mUrl);
    enableControllerButton();

    mWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
        String contentDisposition, String mimetype,
        long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });
}



// Property access
public void setUrl(String url) {
    mUrl = url;
}




/*
 * Internal functions This section contain private and protected level
 * functions which function is only used inside this class or its subclass
 */

// Init webView, website use javascript, so enable javascript
// is a must
@
SuppressLint("SetJavaScriptEnabled")
private void initWebView(View view) {
    // Buttons setups
    mBtnBack = (Button) view.findViewById(R.id.btn_back);
    mBtnForward = (Button) view.findViewById(R.id.btn_forward);
    mBtnRefresh = (Button) view.findViewById(R.id.btn_refresh);
    // Setup for button controller
    mBtnRefresh.setOnClickListener(new OnClickListener() {@
        Override
        public void onClick(View v) {
            mWebView.reload();
            enableControllerButton();
        }
    });
    mBtnBack.setOnClickListener(new OnClickListener() {@
        Override
        public void onClick(View v) {
            mWebView.goBack();
            enableControllerButton();
        }
    });
    mBtnForward.setOnClickListener(new OnClickListener() {

        @
        Override
        public void onClick(View v) {
            mWebView.goForward();
            enableControllerButton();
        }
    });

    initClient();
    mWebView = (WebView) view.findViewById(R.id.webView);
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    mWebView.setWebViewClient(mWebViewClient);
}

/*
 * Internal functions
 */
private void enableControllerButton() {
    if (mIsLoadFinish) {
        mBtnRefresh.setEnabled(true);
        if (mWebView.canGoBack()) {
            mBtnBack.setEnabled(true);
        } else {
            mBtnBack.setEnabled(false);
        }
        if (mWebView.canGoForward()) {
            mBtnForward.setEnabled(true);
        } else {
            mBtnForward.setEnabled(false);
        }
    } else {
        mBtnBack.setEnabled(false);
        mBtnForward.setEnabled(false);
    }
}

private void initClient() {
    mWebViewClient = new WebViewClient() {@
        Override
        public void onPageFinished(WebView view, String url) {
            mIsLoadFinish = true;
            enableControllerButton();
            Log.d(TAG, "Finish loading");
        }
    };
}

}

2 个答案:

答案 0 :(得分:1)

    webView.setWebViewClient(new WebViewClient()
    {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        { 
            if (url.contains("market://"))
            {
                    try
                    {
                        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    } catch (android.content.ActivityNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                    return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    });
    webView.loadUrl(url);

答案 1 :(得分:0)

使您的应用程序链接如下,

GoogleCamera application link

这个链接在Playstore的任何地方开放。

有关详细信息,请参阅answer