GVKun编程网logo

java-使用android的本机Camera App捕获照片会抛出SecurityException(android捕获异常)

11

在这里,我们将给大家分享关于java-使用android的本机CameraApp捕获照片会抛出SecurityException的知识,让您更了解android捕获异常的本质,同时也会涉及到如何更有效

在这里,我们将给大家分享关于java-使用android的本机Camera App捕获照片会抛出SecurityException的知识,让您更了解android捕获异常的本质,同时也会涉及到如何更有效地Android 2.3.x javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚、Android 9.0 自动更新 安装包解析错误 java.lang.SecurityException: Permission Denial、Android getActiveNotifications抛出SecurityException、Android KSOAP2 SSL java.security.cert.CertPathValidatorException的内容。

本文目录一览:

java-使用android的本机Camera App捕获照片会抛出SecurityException(android捕获异常)

java-使用android的本机Camera App捕获照片会抛出SecurityException(android捕获异常)

我正在尝试从android设备的本机Camera App拍照.为此,我首先创建一个文件,然后附加其URI,以捕获图像并将输出写入该文件.
对于文件提供者,我在AndroidManifest文件中添加了以下内容

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.myapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <Meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></Meta-data>
    </provider>

并且用于捕获图像的Java代码如下

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(mActivity.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            String imageName = "image_" + Preferences.getStringForUserId(mActivity);
            photoFile = createImageFile(imageName);
            mCurrentPhotoPath = photoFile.getAbsolutePath();

        } catch (IOException ex) {
            // Error occurred while creating the File
            ex.printstacktrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            try {
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    String authorities = "com.example.myapp.fileprovider";
                    Uri photoURI = FileProvider.getUriForFile(mActivity, authorities, photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

                } else {
                    Uri photoURI = Uri.fromFile(photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                }
                mActivity.startActivityForResult(takePictureIntent, CAMERA_CODE);

            } catch (Exception ex) {
                // Error occurred while creating the File
                ex.printstacktrace();
                Toast.makeText(mActivity, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    } else {
        Toast.makeText(mActivity, "No Camera App found in device", Toast.LENGTH_LONG).show();
    }

如Google在Android自己的文档中所述:https://developer.android.com/training/camera/photobasics.html
我添加了必要的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但是没有添加Camera的权限,我也正在要求Android M或更高版本的运行时权限.
从Google文档获得的信息很明显,如果我们想使用本机Camera App来为我们的App捕获照片,则不需要“ Camera Permission”,那么为什么它会给我以下所述的例外情况

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Android/data/com.example.myapp/files/Pictures/myapp/image_20130-1383873550.jpg} (has extras) } from ProcessRecord{d82d864 6641:com.pencilinapp.pencilin/u0a63} (pid=6641, uid=10063) with revoked permission android.permission.CAMERA

所以我的问题是,从现在开始我是否必须同时要求“相机”和“存储”权限?为什么在Google文档中没有说明?

我已经研究并检查了许多stackoverflow线程,但到目前为止,没有一个人给出与该问题有关的答案.

解决方法:

问题是撤销的权限(“具有撤销的权限android.permission.CAMERA”).通常,您不需要权限,但是如果您请求权限(例如,在您的应用的先前版本中),并且用户撤消了该权限,则您不能使用ACTION_IMAGE_CAPTURE.

Android 2.3.x javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚

Android 2.3.x javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚

我只在(可能是一些)2.3.x设备上收到此错误.它适用于运行 Android版本的任何其他设备.

这是我的HTTPRequestController:

public class HttpRequestController {

private final static String TAG = "HttpRequestController";

private static HttpRequestController instance;

public enum Method {
    PUT,POST,DELETE,GET
}

private HttpRequestController() {

}

public static HttpRequestController getInstance() {
    if (instance == null)
        instance = new HttpRequestController();

    return instance;
}

public String doRequest(String url,HashMap<Object,Object> data,Method method,String token) throws Exception {

    InputStream certificateInputStream = null;
    if (MyApplication.PRODUCTION) {
        certificateInputStream = MyApplication.context
                .getResources().openRawResource(R.raw.production_cert);
        LogUtils.log("using production SSL certificate");
    } else {
        certificateInputStream = MyApplication.context
                .getResources().openRawResource(R.raw.staging_cert);
        LogUtils.log("using staging SSL certificate");
    }

    KeyStore trustStore = KeyStore.getInstance("BKS");
    try{
    trustStore.load(certificateInputStream,"re3d6Exe5HBsdskad8efj8CxZwv".tochararray());
    } finally {
        certificateInputStream.close();
    }


    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trustStore);
    LogUtils.log("SSL: did init TrustManagerFactory with trust keyStore");
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null,tmf.getTrustManagers(),null);
    LogUtils.log("SSL: did init context with trust keyStore");  


    URL request = new URL(url);
    HttpsURLConnection urlConnection = (HttpsURLConnection) request
            .openConnection();

    LogUtils.log("SSL: did open HttpsURLConnection");   

    urlConnection.setHostnameVerifier(new StrictHostnameVerifier());
    urlConnection.setSSLSocketFactory(context.getSocketFactory());
    urlConnection.setConnectTimeout(15000);
    LogUtils.log("SSL: did set Factory and Timeout.");

    if (method != Method.GET){
        urlConnection.setDoOutput(true);
    }
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty("Content-Type","application/json");
        urlConnection.setRequestProperty("Accept","application/json");

    LogUtils.log("SSL: urlConnection did set request properties.");

    if (token != null) {
        urlConnection.setRequestProperty("Authorization","Token " + token);
    }
        urlConnection.setRequestMethod(method.toString());
        urlConnection.connect();

        LogUtils.log("SSL: urlConnection did connect.");

    if (method != Method.GET) {
        ObjectMapper mapper = new ObjectMapper();
        String jsonValue = mapper.writeValueAsstring(data);
        OutputStream os = urlConnection.getoutputStream();
        os.write(jsonValue.getBytes());
        os.flush();
        LogUtils.log(TAG,"Params: " + jsonValue);
    }

    LogUtils.log(TAG,method.toString() + ": " + url);

    InputStream in = null;
    if (urlConnection.getResponseCode() == 200) {
        in = urlConnection.getInputStream();
    } else {
        in = urlConnection.getErrorStream();
    }
    String response = convertStreamToString(in);

    LogUtils.log(TAG,"Got response : " + url);
    LogUtils.log(TAG,"Response : " + response);

    return response;
}

public String convertStreamToString(InputStream inputStream) {
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(
            inputStream));
    StringBuilder stringBuilder = new StringBuilder();

    String line = null;
    try {
        while ((line = buffReader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }
    } catch (IOException e) {
        e.printstacktrace();
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printstacktrace();
        }
    }
    return stringBuilder.toString();
}

public HttpClient retrieveHttpClient() {
    return new MyHttpClient(MyApplication.context);
}

}

当我运行命令时:

openssl s_client -debug -connect www.mysitedomain.com:443

我收到了回复:

--
some key stuff 
--
Certificate chain
 0 s:/OU=Domain Control Validated/CN=www.mydomainname.com
   i:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2
 1 s:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
 2 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
 some more certificate stuff
-----END CERTIFICATE-----

ubject=/OU=Domain Control Validated/CN=www.mydomainname.com
issuer=/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2
---
No client certificate CA names sent
---
SSL handshake has read 4091 bytes and written 328 bytes
---
New,TLSv1/SSLv3,Cipher is DHE-RSA-AES256-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1
    Cipher    : DHE-RSA-AES256-SHA
    Session-ID: 57C379C59483809A7FE1BF8E235C5BFA7789E62AAEBCA9BC14B5273F5D1304E7
    Session-ID-ctx: 
    Master-Key: 6FCD498D1294415A42B57420F0C05AB903EF8E56CB6F1530390F73AF5E4CBC22B359D5CDA09811E075A5C598002C380D
    Key-Arg   : None
    Start Time: 1390473282
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

所以它返回没问题…但它仍然给我测试的2.3.x设备的这个错误.

在此之后我得到一个例外:

LogUtils.log("SSL: urlConnection did set request properties.");

这是例外:

01-23 10:20:28.459: W/System.err(1623): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:477)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:328)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.luni.internal.net.www.protocol.http.httpconnection.setupSecureSocket(httpconnection.java:185)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:433)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:378)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
01-23 10:20:28.459: W/System.err(1623):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:152)

我称之为的方式是:

String response = HttpRequestController
                            .getInstance()
                            .doRequest(ApiUrls.LOGIN,params,Method.POST,null);

它适用于运行2.3.x以上Android版本的任何其他设备(根据我的测试).

Android文档似乎没有关于2.3兼容性的主题.

解决方法

您必须告诉Android系统信任您的证书.您的问题是2.3之后的Android接受了您的证书,因为它包含在可信证书列表中,但是之前的版本不包括在内,因此,存在问题.

我建议你在Android文档上做:

// Load CAs from an InputStream
// (Could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
try {
    ca = cf.generateCertificate(caInput);
    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
    caInput.close();
}

// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null,null);
keyStore.setCertificateEntry("ca",ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null,null);

// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL("https://certs.cac.washington.edu/CAtest/");
HttpsURLConnection urlConnection =
    (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in,System.out);

我也在做同样的事情,它在每台设备上运行正常,使用Android 2.3及以下版本,我的网站证书是私有的.

试试吧,告诉我它现在是否正常工作.

希望它能帮到你!

Android 9.0 自动更新 安装包解析错误 java.lang.SecurityException: Permission Denial

Android 9.0 自动更新 安装包解析错误 java.lang.SecurityException: Permission Denial

在 android 9.0 自动下载更新时 遇到 安装包解析错误
在这里插入图片描述
在 android 7.0 使用的是同一套框架却没有问题?
然后通过 adb 命令抓取一下异常发现


12-18 19:21:32.665  4804  5604 W InstallStaging: java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.***.***.update_app.file_provider/download/update/***.apk from pid=4804, uid=1000 requires the provider be exported, or grantUriPermission()
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.os.Parcel.createException(Parcel.java:1950)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.os.Parcel.readException(Parcel.java:1918)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1459)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1296)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.content.ContentResolver.openInputStream(ContentResolver.java:1016)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:167)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:160)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.os.AsyncTask$2.call(AsyncTask.java:333)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
12-18 19:21:32.665  4804  5604 W InstallStaging: 	at java.lang.Thread.run(Thread.java:764)

发现 Android9.0 是需要权限读取 fileProvider 和 uri 的
于是我看了一下三方框架的源码,确实没有加权限

private Intent installIntent(Context context, String path) {
   
   
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String fileProviderAuthority = getFileProviderAuthority(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && null!= fileProviderAuthority) {
   
   
            Uri fileUri = FileProvider.getUriForFile(context, fileProviderAuthority, new File(path));
            intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
   
   
            intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
        }
        return intent;
    }

于是我就扒了一下源码对其进行修改

private Intent installIntent(Context context, String path) {
   
   
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String fileProviderAuthority = getFileProviderAuthority(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && null!= fileProviderAuthority) {
   
   
            Uri fileUri = FileProvider.getUriForFile(context, fileProviderAuthority, new File(path));
            intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
            grantUriPermission(getPackageName(), fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        } else {
   
   
            intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
        }
        return intent;
    }

测试通过,完美安装,没有出现解析失败问题

代码:

		<!--AndroidManifest.xml-->
		<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
		<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   	    <uses-permission android:name="android.permission.INTERNET"/>
		
		<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="包名.file_provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <!-- 元数据 -->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                tools:replace="android:resource" />
        </provider> 
        
		<service android:name=".UpdateService"/>//从源码下载的升级下载安装apk的服务
 <!-- file_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="." name="download" />
    <external-cache-path
        name="storage/emulated/0"
        path="." />
</paths>
/**
 * Created by Sun on 2020/12/19.
 */
public class UpdateService extends Service {
   
   
    public static final String TAG = "UpdateService";
    public static final String ACTION = "me.shenfan.UPDATE_APP";
    public static final String STATUS = "status";
    public static final String PROGRESS = "progress";
    public static boolean DEBUG = false;

    //下载大小通知频率
    public static final int UPDATE_NUMBER_SIZE = 1;
    public static final int DEFAULT_RES_ID = -1;

    public static final int UPDATE_PROGRESS_STATUS = 0;
    public static final int UPDATE_ERROR_STATUS = -1;
    public static final int UPDATE_SUCCESS_STATUS = 1;

    //params
    private static final String URL = "downloadUrl";
    private static final String ICO_RES_ID = "icoResId";
    private static final String ICO_SMALL_RES_ID = "icoSmallResId";
    private static final String UPDATE_PROGRESS = "updateProgress";
    private static final String STORE_DIR = "storeDir";
    private static final String DOWNLOAD_NOTIFICATION_FLAG = "downloadNotificationFlag";
    private static final String DOWNLOAD_SUCCESS_NOTIFICATION_FLAG = "downloadSuccessNotificationFlag";
    private static final String DOWNLOAD_ERROR_NOTIFICATION_FLAG = "downloadErrorNotificationFlag";
    private static final String IS_SEND_BROADCAST = "isSendBroadcast";


    private String downloadUrl;
    private int icoResId;             //default app ico
    private int icoSmallResId;
    private int updateProgress;   //update notification progress when it add number
    private String storeDir;          //default sdcard/Android/package/update
    private int downloadNotificationFlag;
    private int downloadSuccessNotificationFlag;
    private int downloadErrorNotificationFlag;
    private boolean isSendBroadcast;

    private UpdateProgressListener updateProgressListener;
    private LocalBinder localBinder = new LocalBinder();

    /**
     * Class used for the client Binder.
     */
    public class LocalBinder extends Binder {
   
   
        /**
         * set update progress call back
         *
         * @param listener
         */
        public void setUpdateProgressListener(UpdateProgressListener listener) {
   
   
            UpdateService.this.setUpdateProgressListener(listener);
        }
    }


    private boolean startDownload;//开始下载
    private int lastProgressNumber;
    private NotificationCompat.Builder builder;
    private NotificationManager manager;
    private int notifyId;
    private String appName;
    private LocalBroadcastManager localBroadcastManager;
    private Intent localIntent;
    private DownloadApk downloadApkTask;

    /**
     * whether debug
     */
    public static void debug() {
   
   
        DEBUG = true;
    }

    private Intent installIntent(Context context, String path) {
   
   
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String fileProviderAuthority = getFileProviderAuthority(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && null!= fileProviderAuthority) {
   
   
            Uri fileUri = FileProvider.getUriForFile(context, fileProviderAuthority, new File(path));
            intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
            grantUriPermission(getPackageName(), fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        } else {
   
   
            intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
        }
        return intent;
    }

    /**
     * 获取FileProvider的auth
     */
    private static String getFileProviderAuthority(Context context) {
   
   
        try {
   
   
            for (ProviderInfo provider : context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PROVIDERS).providers) {
   
   
                if (FileProvider.class.getName().equals(provider.name) && provider.authority.endsWith(".update_app.file_provider")) {
   
   
                    return provider.authority;
                }
            }
        } catch (PackageManager.NameNotFoundException ignore) {
   
   
        }
        return null;
    }

    private static Intent webLauncher(String downloadUrl) {
   
   
        Uri download = Uri.parse(downloadUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, download);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    private static String getSaveFileName(String downloadUrl) {
   
   
        if (downloadUrl == null || TextUtils.isEmpty(downloadUrl)) {
   
   
            return System.currentTimeMillis() + ".apk";
        }
        return downloadUrl.substring(downloadUrl.lastIndexOf("/"));
    }

    private static File getDownloadDir(UpdateService service) {
   
   
        File downloadDir = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
   
   
            if (service.storeDir != null) {
   
   
                downloadDir = new File(Environment.getExternalStorageDirectory(), service.storeDir);
            } else {
   
   
                downloadDir = new File(service.getExternalCacheDir(), "update");
            }
        } else {
   
   
            downloadDir = new File(service.getCacheDir(), "update");
        }
        if (!downloadDir.exists()) {
   
   
            downloadDir.mkdirs();
        }
        return downloadDir;
    }


    @Override
    public void onCreate() {
   
   
        super.onCreate();
        appName = getApplicationName();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
   
   
        if (!startDownload && intent != null) {
   
   
            startDownload = true;
            downloadUrl = intent.getStringExtra(URL);
            icoResId = intent.getIntExtra(ICO_RES_ID, DEFAULT_RES_ID);
            icoSmallResId = intent.getIntExtra(ICO_SMALL_RES_ID, DEFAULT_RES_ID);
            storeDir = intent.getStringExtra(STORE_DIR);
            updateProgress = intent.getIntExtra(UPDATE_PROGRESS, UPDATE_NUMBER_SIZE);
            downloadNotificationFlag = intent.getIntExtra(DOWNLOAD_NOTIFICATION_FLAG, 0);
            downloadErrorNotificationFlag = intent.getIntExtra(DOWNLOAD_ERROR_NOTIFICATION_FLAG, 0);
            downloadSuccessNotificationFlag = intent.getIntExtra(DOWNLOAD_SUCCESS_NOTIFICATION_FLAG, 0);
            isSendBroadcast = intent.getBooleanExtra(IS_SEND_BROADCAST, false);


            if (DEBUG) {
   
   
                Log.d(TAG, "downloadUrl: " + downloadUrl);
                Log.d(TAG, "icoResId: " + icoResId);
                Log.d(TAG, "icoSmallResId: " + icoSmallResId);
                Log.d(TAG, "storeDir: " + storeDir);
                Log.d(TAG, "updateProgress: " + updateProgress);
                Log.d(TAG, "downloadNotificationFlag: " + downloadNotificationFlag);
                Log.d(TAG, "downloadErrorNotificationFlag: " + downloadErrorNotificationFlag);
                Log.d(TAG, "downloadSuccessNotificationFlag: " + downloadSuccessNotificationFlag);
                Log.d(TAG, "isSendBroadcast: " + isSendBroadcast);
            }

            notifyId = startId;
            buildNotification();
            buildBroadcast();
            downloadApkTask = new DownloadApk(this);
            downloadApkTask.execute(downloadUrl);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
   
   
        return localBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
   
   
        return true;
    }

    public void setUpdateProgressListener(UpdateProgressListener updateProgressListener) {
   
   
        this.updateProgressListener = updateProgressListener;
    }

    @Override
    public void onDestroy() {
   
   
        if (downloadApkTask != null) {
   
   
            downloadApkTask.cancel(true);
        }

        if (updateProgressListener != null) {
   
   
            updateProgressListener = null;
        }
        localIntent = null;
        builder = null;

        super.onDestroy();
    }

    public String getApplicationName() {
   
   
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
   
   
            packageManager = getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
   
   
            applicationInfo = null;
        }
        String applicationName =
                (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

    private void buildBroadcast() {
   
   
        if (!isSendBroadcast) {
   
   
            return;
        }
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localIntent = new Intent(ACTION);
    }

    private void sendLocalBroadcast(int status, int progress) {
   
   
        if (!isSendBroadcast || localIntent == null) {
   
   
            return;
        }
        localIntent.putExtra(STATUS, status);
        localIntent.putExtra(PROGRESS, progress);
        localBroadcastManager.sendBroadcast(localIntent);
    }

    private void buildNotification() {
   
   
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(getString(R.string.update_app_model_prepare, appName))
                .setWhen(System.currentTimeMillis())
                .setProgress(100, 1, false)
                .setSmallIcon(icoSmallResId)
                .setLargeIcon(BitmapFactory.decodeResource(
                        getResources(), icoResId))
                .setDefaults(downloadNotificationFlag);

        manager.notify(notifyId, builder.build());
    }

    private void start() {
   
   
        builder.setContentTitle(appName);
        builder.setContentText(getString(R.string.update_app_model_progress, 1, "%"));
        //setChannelId 必须添加否则不能在通知栏显示(Android 8.0)
        builder.setChannelId(getPackageName());
        manager.notify(notifyId, builder.build());
        sendLocalBroadcast(UPDATE_PROGRESS_STATUS, 1);
        if (updateProgressListener != null) {
   
   
            updateProgressListener.start();
        }
    }

    /**
     * @param progress download percent , max 100
     */
    private void update(int progress) {
   
   
        if (progress - lastProgressNumber > updateProgress) {
   
   
            lastProgressNumber = progress;
            builder.setProgress(100, progress, false);
            builder.setContentText(getString(R.string.update_app_model_progress, progress, "%"));
            manager.notify(notifyId, builder.build());
            sendLocalBroadcast(UPDATE_PROGRESS_STATUS, progress);
            if (updateProgressListener != null) {
   
   
                updateProgressListener.update(progress);
            }
        }
    }

    private void success(String path) {
   
   
        builder.setProgress(0, 0, false);
        builder.setContentText(getString(R.string.update_app_model_success));
        Intent i = installIntent(this, path);
        PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(intent);
        builder.setDefaults(downloadSuccessNotificationFlag);
        Notification n = builder.build();
        n.contentIntent = intent;
        manager.notify(notifyId, n);
        sendLocalBroadcast(UPDATE_SUCCESS_STATUS, 100);
        if (updateProgressListener != null) {
   
   
            updateProgressListener.success();
        }
        startActivity(i);
        stopSelf();
    }

    private void error() {
   
   
        Intent i = webLauncher(downloadUrl);
        PendingIntent intent = PendingIntent.getActivity(this, 0, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentText(getString(R.string.update_app_model_error));
        builder.setContentIntent(intent);
        builder.setProgress(0, 0, false);
        builder.setDefaults(downloadErrorNotificationFlag);
        Notification n = builder.build();
        n.contentIntent = intent;
        manager.notify(notifyId, n);
        sendLocalBroadcast(UPDATE_ERROR_STATUS, -1);
        if (updateProgressListener != null) {
   
   
            updateProgressListener.error();
        }
        stopSelf();
    }

    private static class DownloadApk extends AsyncTask<String, Integer, String> {
   
   

        private WeakReference<UpdateService> updateServiceWeakReference;

        public DownloadApk(UpdateService service) {
   
   
            updateServiceWeakReference = new WeakReference<>(service);
        }

        @Override
        protected void onPreExecute() {
   
   
            super.onPreExecute();
            UpdateService service = updateServiceWeakReference.get();
            if (service != null) {
   
   
                service.start();
            }
        }

        @Override
        protected String doInBackground(String... params) {
   
   

            final String downloadUrl = params[0];

            final File file = new File(UpdateService.getDownloadDir(updateServiceWeakReference.get()),
                    UpdateService.getSaveFileName(downloadUrl));
            if (DEBUG) {
   
   
                Log.d(TAG, "download url is " + downloadUrl);
                Log.d(TAG, "download apk cache at " + file.getAbsolutePath());
            }
            File dir = file.getParentFile();
            if (!dir.exists()) {
   
   
                dir.mkdirs();
            }

            HttpURLConnection httpConnection = null;
            InputStream is = null;
            FileOutputStream fos = null;
            int updateTotalSize = 0;
            URL url;
            try {
   
   
                url = new URL(downloadUrl);
                httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setConnectTimeout(20000);
                httpConnection.setReadTimeout(20000);

                if (DEBUG) {
   
   
                    Log.d(TAG, "download status code: " + httpConnection.getResponseCode());
                }

                if (httpConnection.getResponseCode() != 200) {
   
   
                    return null;
                }

                updateTotalSize = httpConnection.getContentLength();

                if (file.exists()) {
   
   
                    if (updateTotalSize == file.length()) {
   
   
                        // 下载完成
                        return file.getAbsolutePath();
                    } else {
   
   
                        file.delete();
                    }
                }
                file.createNewFile();
                is = httpConnection.getInputStream();
                fos = new FileOutputStream(file, false);
                byte buffer[] = new byte[4096];

                int readSize = 0;
                int currentSize = 0;

                while ((readSize = is.read(buffer)) > 0) {
   
   
                    fos.write(buffer, 0, readSize);
                    currentSize += readSize;
                    publishProgress((currentSize * 100 / updateTotalSize));
                }
                // download success
            } catch (Exception e) {
   
   
                e.printStackTrace();
                return null;
            } finally {
   
   
                if (httpConnection != null) {
   
   
                    httpConnection.disconnect();
                }
                if (is != null) {
   
   
                    try {
   
   
                        is.close();
                    } catch (IOException e) {
   
   
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
   
   
                    try {
   
   
                        fos.close();
                    } catch (IOException e) {
   
   
                        e.printStackTrace();
                    }
                }
            }
            return file.getAbsolutePath();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
   
   
            super.onProgressUpdate(values);
            if (DEBUG) {
   
   
                Log.d(TAG, "current progress is " + values[0]);
            }
            UpdateService service = updateServiceWeakReference.get();
            if (service != null) {
   
   
                service.update(values[0]);
            }
        }

        @Override
        protected void onPostExecute(String s) {
   
   
            super.onPostExecute(s);
            UpdateService service = updateServiceWeakReference.get();
            if (service != null) {
   
   
                if (s != null) {
   
   
                    service.success(s);
                } else {
   
   
                    service.error();
                }
            }
        }
    }


    /**
     * a builder class helper use UpdateService
     */
    public static class Builder {
   
   

        private String downloadUrl;
        private int icoResId = DEFAULT_RES_ID;             //default app ico
        private int icoSmallResId = DEFAULT_RES_ID;
        private int updateProgress = UPDATE_NUMBER_SIZE;   //update notification progress when it add number
        private String storeDir;          //default sdcard/Android/package/update
        private int downloadNotificationFlag;
        private int downloadSuccessNotificationFlag;
        private int downloadErrorNotificationFlag;
        private boolean isSendBroadcast;

        protected Builder(String downloadUrl) {
   
   
            this.downloadUrl = downloadUrl;
        }

        public static Builder create(String downloadUrl) {
   
   
            if (downloadUrl == null) {
   
   
                throw new NullPointerException("downloadUrl == null");
            }
            return new Builder(downloadUrl);
        }

        public String getDownloadUrl() {
   
   
            return downloadUrl;
        }

        public int getIcoResId() {
   
   
            return icoResId;
        }

        public Builder setIcoResId(int icoResId) {
   
   
            this.icoResId = icoResId;
            return this;
        }

        public int getIcoSmallResId() {
   
   
            return icoSmallResId;
        }

        public Builder setIcoSmallResId(int icoSmallResId) {
   
   
            this.icoSmallResId = icoSmallResId;
            return this;
        }

        public int getUpdateProgress() {
   
   
            return updateProgress;
        }

        public Builder setUpdateProgress(int updateProgress) {
   
   
            if (updateProgress < 1) {
   
   
                throw new IllegalArgumentException("updateProgress < 1");
            }
            this.updateProgress = updateProgress;
            return this;
        }

        public String getStoreDir() {
   
   
            return storeDir;
        }

        public Builder setStoreDir(String storeDir) {
   
   
            this.storeDir = storeDir;
            return this;
        }

        public int getDownloadNotificationFlag() {
   
   
            return downloadNotificationFlag;
        }

        public Builder setDownloadNotificationFlag(int downloadNotificationFlag) {
   
   
            this.downloadNotificationFlag = downloadNotificationFlag;
            return this;
        }

        public int getDownloadSuccessNotificationFlag() {
   
   
            return downloadSuccessNotificationFlag;
        }

        public Builder setDownloadSuccessNotificationFlag(int downloadSuccessNotificationFlag) {
   
   
            this.downloadSuccessNotificationFlag = downloadSuccessNotificationFlag;
            return this;
        }

        public int getDownloadErrorNotificationFlag() {
   
   
            return downloadErrorNotificationFlag;
        }

        public Builder setDownloadErrorNotificationFlag(int downloadErrorNotificationFlag) {
   
   
            this.downloadErrorNotificationFlag = downloadErrorNotificationFlag;
            return this;
        }

        public boolean isSendBroadcast() {
   
   
            return isSendBroadcast;
        }

        public Builder setIsSendBroadcast(boolean isSendBroadcast) {
   
   
            this.isSendBroadcast = isSendBroadcast;
            return this;
        }

        public Builder build(Context context) {
   
   
            if (context == null) {
   
   
                throw new NullPointerException("context == null");
            }
            Intent intent = new Intent();
            intent.setClass(context, UpdateService.class);
            intent.putExtra(URL, downloadUrl);

            if (icoResId == DEFAULT_RES_ID) {
   
   
                icoResId = getIcon(context);
            }

            if (icoSmallResId == DEFAULT_RES_ID) {
   
   
                icoSmallResId = icoResId;
            }
            intent.putExtra(ICO_RES_ID, icoResId);
            intent.putExtra(STORE_DIR, storeDir);
            intent.putExtra(ICO_SMALL_RES_ID, icoSmallResId);
            intent.putExtra(UPDATE_PROGRESS, updateProgress);
            intent.putExtra(DOWNLOAD_NOTIFICATION_FLAG, downloadNotificationFlag);
            intent.putExtra(DOWNLOAD_SUCCESS_NOTIFICATION_FLAG, downloadSuccessNotificationFlag);
            intent.putExtra(DOWNLOAD_ERROR_NOTIFICATION_FLAG, downloadErrorNotificationFlag);
            intent.putExtra(IS_SEND_BROADCAST, isSendBroadcast);
            context.startService(intent);

            return this;
        }

        private int getIcon(Context context) {
   
   

            final PackageManager packageManager = context.getPackageManager();
            ApplicationInfo appInfo = null;
            try {
   
   
                appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
   
   
                e.printStackTrace();
            }
            if (appInfo != null) {
   
   
                return appInfo.icon;
            }
            return 0;
        }
    }

}

/**
 * 下载回调
 */
public interface UpdateProgressListener {
   
   
    /**
     * download start
     */
     void start();

    /**
     * update download progress
     * @param progress
     */
     void update(int progress);

    /**
     * download success
     */
     void success();

    /**
     * download error
     */
     void error();
}

			UpdateService.Builder.create("服务器给的下载路径")
                                 .setStoreDir("下载路径")//default sdcard/Android/package/update
               				     .setIsSendBroadcast(true)
                                 .setIcoResId(android.R.drawable.ic_notification_clear_all)
                                 .setIcoSmallResId(android.R.drawable.ic_notification_overlay)
                                 .build(context);

三方框架源码地址

Android getActiveNotifications抛出SecurityException

Android getActiveNotifications抛出SecurityException

我有一个监听通知的NotificationService类.但是,当我调用getActiveNotifications()时,它会抛出SecurityException.是的,我在调用此方法之前已经检查了许可.

我在NotificationService中使用AsyncTask来获取通知.代码如下.

private class AsyncProcessNotification extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... params) {
            int notificationType = MainApplication.settingNotificationType;
            MainApplication.clearNotificationItems();

            if (MainApplication.settingNotificationType == Constants.KEY_NOTIFICATION_TYPE_disABLED) {
                Log.i(TAG,"Notifications disabled");
                return null;
            }

            if (PermissionHelper.isNotificationPermissionGranted(getApplicationContext())) {
                if (getActiveNotifications() == null || getActiveNotifications().length == 0) {
                    Log.i(TAG,"No notifications found");
                    return null;
                }

                Log.i(TAG,"Getting " + getActiveNotifications().length +" notifications");
                Log.i(TAG,"Notification type  " + notificationType);
                for (StatusBarNotification statusBarNotification : getActiveNotifications()) {
                    // Process notifications
                }
            } else {
                //
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Intent notify = new Intent(Constants.ACTION_NOTIFICATION_LISTENER);
            sendbroadcast(notify);
        }
    }

奇怪的是根据
Crashlytics,有时它在if(getActiveNotifications()== null || getActiveNotifications().length == 0)失败,有时它在Log.i失败(TAG,“获取”getActiveNotifications().length“notifications”);

要检查权限,我使用以下方法.

public static boolean isNotificationPermissionGranted(Context context) {
        Set<String> appList = notificationmanagerCompat.getEnabledListenerPackages(context);
        for (String l:appList) {
            if (l.equals(context.getPackageName())) {
                return true;
            }
        }

        return false;
    }

堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:309)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)
Caused by java.lang.SecurityException: disallowed call from unkNown notification listener: android.service.notification.INotificationListener$Stub$Proxy@3e9880d
       at android.os.Parcel.readException(Parcel.java:1599)
       at android.os.Parcel.readException(Parcel.java:1552)
       at android.app.Inotificationmanager$Stub$Proxy.getActiveNotificationsFromListener(Inotificationmanager.java:1046)
       at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:467)
       at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:420)
       at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:120)
       at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:97)
       at android.os.AsyncTask$2.call(AsyncTask.java:295)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)

我知道我必须做一些愚蠢但却无法弄清楚是什么.有人可以帮忙吗?谢谢!

解决方法

I hit the same error in my notification listener service. I was able
to solve it by waiting for the listener service to get connected to the
notification manager before calling any of the
NotificationListenerService methods.

@Override
public void onListenerConnected() {
    Log.i(TAG,"Listener connected");
    listenerConnected = true;
}

private class AsyncProcessNotification extends AsyncTask<Void,Void> {
    protected Void doInBackground(Void... params) {
              ...
            // wait until our notification listener service is connected
            // to the notification manager
            Log.i(TAG,"Waiting for listener to be connected...");

            while(!listenerConnected);

            // Call getActiveNotifications after this
            for (StatusBarNotification sbn : getActiveNotifications()) {
                   processNotifications(sbn);
            }
    }

    // rest of the AsyncTask here
}

Android KSOAP2 SSL java.security.cert.CertPathValidatorException

Android KSOAP2 SSL java.security.cert.CertPathValidatorException

我尝试通过SSL连接到我的JAX-WS服务.没有SSL一切正常.

AsyncTask中的方法:

     HttpsTransportSE  androidHttpTransport = new HttpsTransportSE("10.0.2.2", 8181, "/Server/?wsdl", 10000);
             ((HttpsServiceConnectionSE) androidHttpTransport.getServiceConnection()).setSSLSocketFactory(trustAllHosts()
.getSocketFactory());

             //androidHttpTransport.debug=true;

             androidHttpTransport.call(getSoapAction(method), envelope);

获取SSLContext

public SSLContext allowAllSSL() {
            SSLContext context = null; 
            TrustManager[] trustManagers = null;
            try{
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

             KeyStore keyStore = KeyStore.getInstance("pkcs12");
             InputStream in = cntx.getResources().openRawResource(R.raw.client_keystore);
             try {
             keyStore.load(in, "password".tochararray());
             } catch (CertificateException e) {
             // Todo Auto-generated catch block
             e.printstacktrace();
             } finally {
             in.close();
             }
             tmf.init(keyStore);


                if (trustManagers == null) { 
                        trustManagers = new TrustManager[] { new Fakex509trustmanager() }; 
                } 

                try { 
                        context = SSLContext.getInstance("SSL"); 
                        context.init(null, tmf.getTrustManagers(), new SecureRandom()); 
                } catch (NoSuchAlgorithmException e) { 
                        e.printstacktrace(); 
                } catch (KeyManagementException e) { 
                        e.printstacktrace(); 
                } 

           HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
           HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
               public boolean verify(String hostname, SSLSession session) {
                    return true;
                  }
                });
            }catch(Exception ex)
            {
                Log.e(TAG,"allowAllSSL Failed: "+ex.toString());
            }
           return context;
        } 

我收到此错误日志:

12-18 07:51:42.161: E/Example:logonAsync(3161): doInBackground Failed: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
12-18 07:51:42.161: W/System.err(3161): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
12-18 07:51:42.169: W/System.err(3161):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:401)
12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.httpconnection.setupSecureSocket(httpconnection.java:209)
12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)

解决方法:

我发现问我的问题:在MainAsync中:

 HttpsTransportSE  androidHttpTransport = new HttpsTransportSE(10.0.2.2, 8181, "/server/?wsdl", 10000);
             ((HttpsServiceConnectionSE) androidHttpTransport.getServiceConnection()).setSSLSocketFactory(trustAllHosts().getSocketFactory());


protected  SSLContext trustAllHosts()
{   
    return allowAllSSL();
}

 public SSLContext allowAllSSL() {
        SSLContext context = null; 
        TrustManager[] trustManagers = null;
        KeyManagerFactory mgrFact;
        try{
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        mgrFact = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

         KeyStore keyStore = KeyStore.getInstance("pkcs12");
         InputStream in = cntx.getResources().openRawResource(R.raw.keystore);
         try {
         keyStore.load(in, "password".tochararray());
         mgrFact.init(keyStore, "password".tochararray());
         } catch (CertificateException e) {
         // Todo Auto-generated catch block
         e.printstacktrace();
         } finally {
         in.close();
         }
         tmf.init(keyStore);


            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() 
            {    
                    @Override 
                    public boolean verify(String hostname, SSLSession session) { 

                            return true; 
                    } 

            }); 


            if (trustManagers == null) { 
                    trustManagers = new TrustManager[] { new Fakex509trustmanager() }; 
            }

            final TrustManager[] trustAllCerts = new TrustManager[] {new x509trustmanager() {
                public X509Certificate[] getAcceptedissuers() {
                    System.out.println("getAcceptedissuers");
                     return null;
                }
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                    System.out.println("Сведения о сертификате : " +       chain[0].getIssuerX500Principal().getName() + "\n Тип авторизации : " + authType);
                }
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                    System.out.println("checkClientTrusted : " + authType);
                }
            } };
            //tmf.getTrustManagers()
            try { 
                    context = SSLContext.getInstance("TLS"); 
                    context.init(mgrFact.getKeyManagers(), trustAllCerts, new SecureRandom()); 
            } catch (NoSuchAlgorithmException e) { 
                    e.printstacktrace(); 
            } catch (KeyManagementException e) { 
                    e.printstacktrace(); 
            } 

       HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
       HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
           public boolean verify(String hostname, SSLSession session) {
                return true;
              }
            });
        }catch(Exception ex)
        {
            Log.e(TAG,"allowAllSSL Failed: "+ex.toString());
        }
       return context;
    } 

关于java-使用android的本机Camera App捕获照片会抛出SecurityExceptionandroid捕获异常的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android 2.3.x javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚、Android 9.0 自动更新 安装包解析错误 java.lang.SecurityException: Permission Denial、Android getActiveNotifications抛出SecurityException、Android KSOAP2 SSL java.security.cert.CertPathValidatorException的相关知识,请在本站寻找。

本文标签: