Recently, two functions will be implemented in the project:
1: click on the H5 link to open the local Alipay to pay.
Second: Click on the H5 button to open the local QQ for temporary conversation.
Speaking of the idea of function realization, the two functions are actually implemented in the same principle, but there are some differences in the details.
First, we need to write a tool method to determine whether the application we need to open is installed on the mobile phone.
/**
* Determine if an application is installed on the phone
*/
public static boolean isAppInstalled(Context context,String pkgName) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals(pkgName)) {
return true;
}
}
}
return false;
}
The above is a general judgment method, just need to pass in context and package name. As for how to know the package name, you can go to the application information inside the mobile phone settings to see, and search for specific mobile phones.
Then we intercept the url in our webView to make a judgment. The code is as follows:
final String qq = "com.tencent.mobileqq";
final String alipay = "com.eg.android.AlipayGphone";
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("alipays")) {
isAppInstalled(view, url, alipay);
} else if (url.startsWith("mqqwpa")) {
isAppInstalled(view, url, qq);
} else {
webView.loadUrl(url);
}
return true;
}
});
//Decide whether to install QQ or Alipay, and open it.
private void isAppInstalled(WebView view, String url, String pkgName) {
if (Util.isAppInstalled(MainActivity.this, pkgName)) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
view.goBack();//Turn off the jumping middle page, which is useless to us.
} else {
if ("com.tencent.mobileqq".equals(pkgName)) {
ToastUtils.showLongSafe("Please install mobile phone first QQ");
} else if ("com.eg.android.AlipayGphone".equals(pkgName)) {
ToastUtils.showLongSafe("Please install Alipay mobile phone first.");
}
view.goBack();
}
}
The main code is so much above. In fact, the principle of arousing other applications within our APP is to use scheme links to pass this link into intemt and use startActivity(intent) to arouse other applications (of course, other applications must be registered in the system first).
In fact, after we clicked the button, the initial link is not a scheme link, but a normal http or https link, but through an intermediate link, and finally jumped to the scheme link, thus arousing the application. So in the above code, the goBack() method is called to turn off the middle page and improve the user experience after calling the application or popping up toast.
I just briefly introduced the principles above, to understand the specific principles can see the following links, I also refer to their implementation methods, and then optimize their own, unified the next call method, reduce code, increase readability and maintainability.
Call up QQ Temporary Session with Web Link
WebView tune up Alipay payment
http://blog.csdn.net/zxz_tsgx/article/details/54408140
http://blog.csdn.net/zhuyu19911016520/article/details/71763900
Testing Alipay client
http://blog.csdn.net/u012045061/article/details/49638737