通过Intent发送电子邮件:SecurityException

San*_*osh 4 android-intent android-securityexception android-6.0-marshmallow

以下是我通过Gmail应用发送电子邮件的方式.

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
        emailIntent.setType("text/html");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Puzzle");
        emailIntent.putExtra(Intent.EXTRA_TEXT, someTextHere));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
         try {
            startActivityForResult(emailIntent, SHARE_PUZZLE_REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            showToast("No application found on this device to perform share action");
        } catch (Exception e) {
            showToast(e.getMessage());
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

它没有启动Gmail应用程序,但显示以下消息.

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SEND typ=text/html cmp=com.google.android.gm/.ComposeActivityGmail (has extras) } from ProcessRecord{8293c64 26854:com.xxx.puzzleapp/u0a383} (pid=26854, uid=10383) not exported from uid 10083
Run Code Online (Sandbox Code Playgroud)

关于SOF的问题很少,建议大多数使用exported = true.但我无法使用此解决方案,因为我正在启动另一个应用程序的活动.你能指导我吗?

Raj*_*har 17

试试这个

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/html");
        final PackageManager pm = this.getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
        String className = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.equals("com.google.android.gm")) {
                className = info.activityInfo.name;

                if(className != null && !className.isEmpty()){
                    break;
                }
            }
        }
        emailIntent.setClassName("com.google.android.gm", className);
Run Code Online (Sandbox Code Playgroud)