#############################################
private native void nativeLoadUrl(String url, Map<String, String> headers);使用的时候:
public void loadUrl(String url, Map<String, String> extraHeaders) { mLoadInitFromJava = true; if (URLUtil.isJavaScriptUrl(url)) { // strip off the scheme and evaluate the string stringByEvaluatingJavaScriptFromString( url.substring("javascript:".length())); } else { nativeLoadUrl(url, extraHeaders); } mLoadInitFromJava = false; }
static JNINativeMethod gBrowserFrameNativeMethods[] = { /* name, signature, funcPtr */ { "nativeCallPolicyFunction", "(II)V", (void*) CallPolicyFunction }, { "nativeCreateFrame", "(Landroid/webkit/WebViewCore;Landroid/content/res/AssetManager;Landroid/webkit/WebBackForwardList;)V", (void*) CreateFrame }, { "nativeDestroyFrame", "()V", (void*) DestroyFrame }, { "nativeStopLoading", "()V", (void*) StopLoading }, { "nativeLoadUrl", "(Ljava/lang/String;Ljava/util/Map;)V", (void*) LoadUrl },
static void LoadUrl(JNIEnv *env, jobject obj, jstring url, jobject headers)
public void loadUrl(LoadUrlParams params) { if (mNativeContentViewCore == 0) return; nativeLoadUrl(mNativeContentViewCore, params.mUrl, params.mLoadUrlType, params.mTransitionType, params.mUaOverrideOption, params.getExtraHeadersString(), params.mPostData, params.mBaseUrlForDataUrl, params.mVirtualUrlForDataUrl, params.mCanLoadLocalResources); }在cpp端的h文件中,会直接继承并声明
class ContentViewCoreImpl : public ContentViewCore, public NotificationObserver {
void LoadUrl( JNIEnv* env, jobject obj, jstring url, jint load_url_type, jint transition_type, jint ua_override_option, jstring extra_headers, jbyteArray post_data, jstring base_url_for_data_url, jstring virtual_url_for_data_url, jboolean can_load_local_resources);这样就省却了声明native method的操作。
void ContentViewCoreImpl::LoadUrl( JNIEnv* env, jobject obj, jstring url, jint load_url_type, jint transition_type, jint ua_override_option, jstring extra_headers, jbyteArray post_data, jstring base_url_for_data_url, jstring virtual_url_for_data_url, jboolean can_load_local_resources) { DCHECK(url); NavigationController::LoadURLParams params( GURL(ConvertJavaStringToUTF8(env, url))); params.load_type = static_cast<NavigationController::LoadURLType>( load_url_type); params.transition_type = PageTransitionFromInt(transition_type); params.override_user_agent = static_cast<NavigationController::UserAgentOverrideOption>( ua_override_option); if (extra_headers) params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers); if (post_data) { std::vector<uint8> http_body_vector; base::android::JavaByteArrayToByteVector(env, post_data, &http_body_vector); params.browser_initiated_post_data = base::RefCountedBytes::TakeVector(&http_body_vector); } if (base_url_for_data_url) { params.base_url_for_data_url = GURL(ConvertJavaStringToUTF8(env, base_url_for_data_url)); } if (virtual_url_for_data_url) { params.virtual_url_for_data_url = GURL(ConvertJavaStringToUTF8(env, virtual_url_for_data_url)); } params.can_load_local_resources = can_load_local_resources; LoadUrl(params); }