2

以下是我通过Gmail应用程序发送电子邮件的方式。通过意向发送电子邮件:SecurityException

 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(); 
     } 

这不是开始的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 

上有SOF对此一些问题,其中大部分是建议使用出口=真。但是当我启动另一个应用程序的活动时,我无法使用此解决方案。你能指导我吗?

+2

类名(com.google.android.gm.ComposeActivityGmail)最近被改变。请检查并提供适当的类名。否则,您可以直接给emailIntent.setPackage(“com.google.android.gm”)而不是emailIntent.setClassName; – Rajasekhar

+0

@Rajasekhar越来越android.content.ActivityNotFoundException:未发现处理意图的活动{act = android.intent.action.VIEW typ = text/plain pkg = com.google.android.gm(有额外功能)} –

回答

13

试试这个

 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); 
2

我想Rajasekhar是对的。在具有与传统的应用程序相同的问题我的情况,我已经看了G中网站上的参考代码,并使用类似这样的东西:

public void composeEmail(String[] addresses, String subject) { 
    Intent intent = new Intent(Intent.ACTION_SENDTO); 
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this 
    intent.putExtra(Intent.EXTRA_EMAIL, addresses); 
    intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    if (intent.resolveActivity(getPackageManager()) != null) { 
        startActivity(intent); 
    } 
} 

而且它没有问题的工作。

PS:在我的情况下,我没有问题的应用程序选择器给用户。它的工作原理与每一个版本的Gmail,相同的代码是你的,打破了应用程序在Gmail中的v6.10.23

+0

这适用于我。谢谢! – Swapnil