android阿里滑块验证码,在Android App中接入HTML5滑块验证
在您的Android App工程的Activity文件中,導入WebView組件的依賴庫。import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
在AndroidManifest.xml配置文件中,設置網頁加載的權限。
說明:如果存在其它HTTP資源調用,也需要增加相應的配置。
...
android:usesCleartextTraffic="true"
...>
在activity_main.xml布局文件中,添加WebView組件。
android:layout_height="match_parent"
android:layout_width="match_parent" />
在Activity文件中,加載HTML5業務頁面。public class MainActivity extends AppCompatActivity {
private WebView testWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// 頁面布局。
testWebview = (WebView) findViewById(R.id.webview);
// 設置屏幕自適應。
testWebview.getSettings().setUseWideViewPort(true);
testWebview.getSettings().setLoadWithOverviewMode(true);
// 建議禁止緩存加載,以確保在攻擊發生時可快速獲取最新的滑動驗證組件進行對抗。
testWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
// 設置不使用默認瀏覽器,而直接使用WebView組件加載頁面。
testWebview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
// 設置WebView組件支持加載JavaScript。
testWebview.getSettings().setJavaScriptEnabled(true);
// 建立JavaScript調用Java接口的橋梁。
testWebview.addJavascriptInterface(new testJsInterface(), "testInterface");
// 加載業務頁面。
testWebview.loadUrl("http://39.x.x.x/demo/");
}
}
在Activity文件中,添加自定義Java接口(testJsInterface),并定義getSlideData方法獲取滑塊數據。import android.webkit.JavascriptInterface;
public class testJsInterface {
@JavascriptInterface
public void getSlideData(String callData) {
System.out.println(callData);
}
}
在Activity的initView()方法中,將所添加的自定義Java接口與JavaScript函數綁定。// 設置WebView組件支持JavaScript。
testWebview.getSettings().setJavaScriptEnabled(true);
// 建立JavaScript調用Java接口的橋梁。
testWebview.addJavascriptInterface(new testJsInterface(), "testInterface");
總結
以上是生活随笔為你收集整理的android阿里滑块验证码,在Android App中接入HTML5滑块验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CF手游神罚套装在哪里获取 CF手游神罚
- 下一篇: 【计算机视觉】深度相机(五)--Kine