美文网首页
Andr 6+ 动态权限申请及HTTPS资源访问

Andr 6+ 动态权限申请及HTTPS资源访问

作者: CodeTriumph | 来源:发表于2020-04-23 14:31 被阅读0次

    Andr 6+ 动态申请权限

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[] {Manifest.permission.CAMERA}, 1);
                }
            }
            //...
        }
    

    Andr 6+ 访问https资源

    package com.example.util;
    
    import android.util.Log;
    
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    public class MyX509TrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    
        public static void ShieldCertificateValidationSly() {
            SSLContext sslContext = null;
            try {
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, new TrustManager[]{new MyX509TrustManager()}, null);
                HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
            } catch (Exception ex) {
                Log.d("Literals", "<Shielding Certificate Validation Error>");
                ex.printStackTrace();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Andr 6+ 动态权限申请及HTTPS资源访问

          本文链接:https://www.haomeiwen.com/subject/cucaihtx.html