如何使用 setWebViewClient 在 android webview 中正确打开 google 播放 html 页面上的链接?
how to properly open google play links on html page in android webview with setWebViewClient?
基本上我的问题是
我怎样才能将这两个代码放在一起才能正常运行!!!
这让我无法理解,所以我会详细解释...
任何帮助将不胜感激!!!
我在 http://s-ka-paidbeats.com/app_tree/ourotherapps
it lists a bunch of apps i have made in google play.
in the webpage html i have them listed with standard links like - https://play.google.com/store/apps/details?id=com.yesorno.app.yesorno
有一个网页
我在我创建的所有 android 应用程序中创建了一个选项卡 "My Other Apps"
这个选项卡是一个网络视图 window 显示相同的内容html 我在上面谈论的页面
- http://s-ka-paidbeats.com/app_tree/ourotherapps
问题是当用户访问我的任何 android 应用程序中的 webview window 并点击我在 html 网页上列出的任何应用程序时(https://play.google.com/store/apps/details?id=) 它将他们带到 google 在网络视图中播放 window 并要求他们登录 google 在网络视图中播放 window (即使他们已经登录google 在他们的设备上播放)...
这对用户来说非常丑陋和烦人。
我想做到这一点,当用户访问我的任何应用程序中的 "my other apps" 选项卡,并单击网络视图中的一个应用程序时 window 它会打开实际的 google 播放应用程序(如果安装了 google 播放)或在设备上安装的默认浏览器中打开(如果未安装 google 播放)
我试图将 html 页面上的所有 (https://play.google.com/store/apps/details?id=) 链接更改为 (market://details?id=) 链接,然后访问了 webview window再次在我的应用程序中,但是这次当我单击列出的任何应用程序时,我只收到页面错误 "page does not exist" window
我已经研究过 setWebViewClient 并且我确信可以使用类似于下面发布的代码的方法来做到这一点
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
} });
我尝试将上面的代码添加到我当前的代码中,但现在我的加载对话框在加载 webview 时不会关闭....它只是永远保持加载....所以我采取了上面的代码片段再次从我的代码中删除,因为我认为我没有把它放在正确的位置,或者我没有正确地将这两个代码整合在一起....
我注意到我当前的代码已经定义了一个 setWebViewClient,所以我不确定我是否允许在同一代码中有两个,或者我是否应该尝试将它与当前的代码结合......我不知道从哪里开始,我已经阅读了几个小时....
这是我当前的代码
package com.yesorno.app.yesorno;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.yesorno.app.yesorno.NetorkConnection;
@SuppressLint("SetJavaScriptEnabled")
public class OtherApps extends AppCompatActivity {
private WebView webView;
NetorkConnection ntwrk_con = new NetorkConnection(this);
ProgressDialog dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView toolsresources5 = (TextView)findViewById(R.id.feedbacktextview);
toolsresources5.setVisibility(View.INVISIBLE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.activity_main_webview);
dialog = new ProgressDialog(OtherApps.this);
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
dialog_box_for_internet();
}
}
public void dialog_box_for_internet() {
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
// dismis_dialog_box_for_internet = true;
AlertDialog.Builder builder = new AlertDialog.Builder(
OtherApps.this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_custom_titile, null);
TextView title = (TextView) view.findViewById(R.id.myTitle);
title.setText("Unable To Connect");
builder.setCustomTitle(view);
builder.setMessage("No Internet Connection")
.setCancelable(false)
.setPositiveButton("Retry",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
new Thread_for_internet().execute();
}
// dialog.cancel();
}
})
.setNegativeButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
// Gridview.super.onBackPressed();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
class Thread_for_internet extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@Override
protected Boolean doInBackground(String... args) {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
dialog_box_for_internet();
}
}
public void webView() {
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
});
dialog.setMessage("Loading All Our Apps...\nPlease wait...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
webView.loadUrl("http://s-ka-paidbeats.com/app_tree/ourotherapps.html");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean bRet=false;//set true is menu selection handled
switch (item.getItemId()) {
case R.id.action_settings_3:
Toast.makeText(this, Html.fromHtml("<big><b>Develeped By S-Ka-Paid</b></big><br>© 2016 S-Ka-Paid"), Toast.LENGTH_LONG).show();
bRet=true;
break;
case R.id.action_settings_4:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
//Try Google play
intent2.setData(Uri.parse("market://details?id=com.yesorno.app.yesorno"));
startActivity(intent2);
bRet=true;
break;
default:
bRet=super.onOptionsItemSelected(item);
}
return bRet;
} }
基本上我的问题是
我怎样才能将这两个代码放在一起才能正常运行!!!
这让我无法解决问题
任何帮助将不胜感激!!!
i have tried to change all the (https://play.google.com/store/apps/details?id=) links to (market://details?id=) links on the html page
不要那样做。使用您开始使用的 google 播放 URL。
i noticed my current code already has a setWebViewClient defined so im not sure if im allowed to have two in the same code or if i am suppose to try and combine it with the current one
你把它们结合起来。您创建一个 WebViewClient
来处理一个 WebView
的所有需求。不难:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// is this a play store URL?
String partialUrl = "/store/apps/details?id=";
if (url.contains(partialUrl)) {
// extract the app id from the URL
int pos = url.indexOf(partialUrl) + partialUrl.length();
String appId = url.substring(pos);
try {
// open the google play app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appId));
OtherApps.this.startActivity(intent);
return true; // we overrode the url load
} catch (ActivityNotFoundException e) {
// no google play app, load URL in device browser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
OtherApps.this.startActivity(intent);
return true;
}
}
return false; // no override, let the webview load this url
}
});
任何正在寻找此问题答案的人...这是正确的代码
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// is this a play store URL?
String partialUrl = "/store/apps/details?id=";
if (url.contains(partialUrl)) {
// extract the app id from the URL
int pos = url.indexOf(partialUrl) + partialUrl.length();
String appId = url.substring(pos);
try {
// open the google play app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appId));
OtherApps.this.startActivity(intent);
return true; // we overrode the url load
} catch (ActivityNotFoundException e) {
// no google play app, load URL in device browser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
OtherApps.this.startActivity(intent);
return true;
}
}
return false; // no override, let the webview load this url
}
});
基本上我的问题是
我怎样才能将这两个代码放在一起才能正常运行!!!
这让我无法理解,所以我会详细解释...
任何帮助将不胜感激!!!
我在 http://s-ka-paidbeats.com/app_tree/ourotherapps
it lists a bunch of apps i have made in google play.
in the webpage html i have them listed with standard links like - https://play.google.com/store/apps/details?id=com.yesorno.app.yesorno
我在我创建的所有 android 应用程序中创建了一个选项卡 "My Other Apps"
这个选项卡是一个网络视图 window 显示相同的内容html 我在上面谈论的页面
- http://s-ka-paidbeats.com/app_tree/ourotherapps
问题是当用户访问我的任何 android 应用程序中的 webview window 并点击我在 html 网页上列出的任何应用程序时(https://play.google.com/store/apps/details?id=) 它将他们带到 google 在网络视图中播放 window 并要求他们登录 google 在网络视图中播放 window (即使他们已经登录google 在他们的设备上播放)...
这对用户来说非常丑陋和烦人。
我想做到这一点,当用户访问我的任何应用程序中的 "my other apps" 选项卡,并单击网络视图中的一个应用程序时 window 它会打开实际的 google 播放应用程序(如果安装了 google 播放)或在设备上安装的默认浏览器中打开(如果未安装 google 播放)
我试图将 html 页面上的所有 (https://play.google.com/store/apps/details?id=) 链接更改为 (market://details?id=) 链接,然后访问了 webview window再次在我的应用程序中,但是这次当我单击列出的任何应用程序时,我只收到页面错误 "page does not exist" window
我已经研究过 setWebViewClient 并且我确信可以使用类似于下面发布的代码的方法来做到这一点
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
} });
我尝试将上面的代码添加到我当前的代码中,但现在我的加载对话框在加载 webview 时不会关闭....它只是永远保持加载....所以我采取了上面的代码片段再次从我的代码中删除,因为我认为我没有把它放在正确的位置,或者我没有正确地将这两个代码整合在一起....
我注意到我当前的代码已经定义了一个 setWebViewClient,所以我不确定我是否允许在同一代码中有两个,或者我是否应该尝试将它与当前的代码结合......我不知道从哪里开始,我已经阅读了几个小时....
这是我当前的代码
package com.yesorno.app.yesorno;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.yesorno.app.yesorno.NetorkConnection;
@SuppressLint("SetJavaScriptEnabled")
public class OtherApps extends AppCompatActivity {
private WebView webView;
NetorkConnection ntwrk_con = new NetorkConnection(this);
ProgressDialog dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView toolsresources5 = (TextView)findViewById(R.id.feedbacktextview);
toolsresources5.setVisibility(View.INVISIBLE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.activity_main_webview);
dialog = new ProgressDialog(OtherApps.this);
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
dialog_box_for_internet();
}
}
public void dialog_box_for_internet() {
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
// dismis_dialog_box_for_internet = true;
AlertDialog.Builder builder = new AlertDialog.Builder(
OtherApps.this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_custom_titile, null);
TextView title = (TextView) view.findViewById(R.id.myTitle);
title.setText("Unable To Connect");
builder.setCustomTitle(view);
builder.setMessage("No Internet Connection")
.setCancelable(false)
.setPositiveButton("Retry",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
if (ntwrk_con.isConnectingToInternet()) {
webView();
} else {
new Thread_for_internet().execute();
}
// dialog.cancel();
}
})
.setNegativeButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
// Gridview.super.onBackPressed();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
class Thread_for_internet extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@Override
protected Boolean doInBackground(String... args) {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
dialog_box_for_internet();
}
}
public void webView() {
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
});
dialog.setMessage("Loading All Our Apps...\nPlease wait...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
webView.loadUrl("http://s-ka-paidbeats.com/app_tree/ourotherapps.html");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean bRet=false;//set true is menu selection handled
switch (item.getItemId()) {
case R.id.action_settings_3:
Toast.makeText(this, Html.fromHtml("<big><b>Develeped By S-Ka-Paid</b></big><br>© 2016 S-Ka-Paid"), Toast.LENGTH_LONG).show();
bRet=true;
break;
case R.id.action_settings_4:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
//Try Google play
intent2.setData(Uri.parse("market://details?id=com.yesorno.app.yesorno"));
startActivity(intent2);
bRet=true;
break;
default:
bRet=super.onOptionsItemSelected(item);
}
return bRet;
} }
基本上我的问题是
我怎样才能将这两个代码放在一起才能正常运行!!!
这让我无法解决问题
任何帮助将不胜感激!!!
i have tried to change all the (https://play.google.com/store/apps/details?id=) links to (market://details?id=) links on the html page
不要那样做。使用您开始使用的 google 播放 URL。
i noticed my current code already has a setWebViewClient defined so im not sure if im allowed to have two in the same code or if i am suppose to try and combine it with the current one
你把它们结合起来。您创建一个 WebViewClient
来处理一个 WebView
的所有需求。不难:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// is this a play store URL?
String partialUrl = "/store/apps/details?id=";
if (url.contains(partialUrl)) {
// extract the app id from the URL
int pos = url.indexOf(partialUrl) + partialUrl.length();
String appId = url.substring(pos);
try {
// open the google play app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appId));
OtherApps.this.startActivity(intent);
return true; // we overrode the url load
} catch (ActivityNotFoundException e) {
// no google play app, load URL in device browser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
OtherApps.this.startActivity(intent);
return true;
}
}
return false; // no override, let the webview load this url
}
});
任何正在寻找此问题答案的人...这是正确的代码
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// is this a play store URL?
String partialUrl = "/store/apps/details?id=";
if (url.contains(partialUrl)) {
// extract the app id from the URL
int pos = url.indexOf(partialUrl) + partialUrl.length();
String appId = url.substring(pos);
try {
// open the google play app
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appId));
OtherApps.this.startActivity(intent);
return true; // we overrode the url load
} catch (ActivityNotFoundException e) {
// no google play app, load URL in device browser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
OtherApps.this.startActivity(intent);
return true;
}
}
return false; // no override, let the webview load this url
}
});