H5拉起安卓app 指定的activity

H5页面代码: 查看游戏详情0

 

在浏览器打开,点击跳转到app指定的页面。

 

app的清单文件:

android:name=".GameDetaiActivity"

android:configChanges="orientation|screenSize|keyboardHidden"

android:hardwareAccelerated="true"

android:screenOrientation="portrait"

android:windowSoftInputMode="adjustPan|stateHidden">

 

 

 

android:host="my.com"

android:path="/game_detail"

android:scheme="youxi" />

 

 

GameDetaiActivity里面写方法:

/**

* 从网页H5跳转过来的

*/

private void getWebData() {

Uri uri = getIntent().getData();

if (uri != null) {

String scheme = uri.getScheme();//youxi

String uriHost = uri.getHost();//my.com

String uriPath = uri.getPath();// /game_detail

 

 

id = Integer.parseInt(Objects.requireNonNull(uri.getQueryParameter("id")));//游戏id

webType = Integer.parseInt(Objects.requireNonNull(uri.getQueryParameter("type")));//

int is_down = Integer.parseInt(Objects.requireNonNull(uri.getQueryParameter("is_down")));

if (1 == is_down) {

isDown = true;//跳转进来就开始自动下载

} else {

isDown = false;//

}

 

LogUtil.e("游戏详情:id=" + id + ";webType=" + webType + ";is_down=" + is_down);

 

}

}

 

若要app内部用webview打开该H5,也能跳转到指定的activity。

// 设置Web视图

wv.setWebViewClient(new MyWebViewClient());

 

// 监听 所有点击的链接,如果拦截到我们需要的,就跳转到相对应的页面。

private class MyWebViewClient extends WebViewClient {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

 

if (TextUtils.isEmpty(url)) {

return false;

}

 

Uri uri = Uri.parse(url);

String scheme = uri.getScheme();//还需要判断host

Log.e("logcat", "网页 scheme=" + scheme + " ;url=" + url);

if (TextUtils.equals("youxi", scheme)) {

 

// Intent intent = new Intent(MainActivity.this, RegisterByPhone.class);

//startActivity(intent);

 

return true;//true,拦截在内部。内部处理掉

} else {

return false;

}

 

 

// return super.shouldOverrideUrlLoading(view, url);

}

 

 

@SuppressLint("SetJavaScriptEnabled")

@Override

public void onPageFinished(WebView view, String url) {

view.getSettings().setJavaScriptEnabled(true);

super.onPageFinished(view, url);

}

}

你可能感兴趣的:(android)