【问题标题】:Mailto Android: 'Unsupported action' errorMailto Android:“不支持的操作”错误
【发布时间】:2015-02-16 03:46:51
【问题描述】:

我是新手,但我的 sn-p 编码有什么问题? 当我选择链接时,我收到错误消息:“当前不支持此操作”。 这是我的代码:

public void addEmail() {

    TextView txt = (TextView) findViewById(R.id.emailtext);

    txt.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v){
            Intent intent = new Intent();
            String uriText =
                    "mailto:youremail@gmail.com" + 
                    "?subject=" + URLEncoder.encode("some subject text here") + 
                    "&body=" + URLEncoder.encode("some text here");

                Uri uri = Uri.parse(uriText);

                Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
                sendIntent.setData(uri);
                startActivity(Intent.createChooser(sendIntent, "Send email")); 

    }});

}

非常感谢!

【问题讨论】:

  • @shkschneider,如何解决错误?
  • 当我还没有在 Android 电子邮件客户端中设置电子邮件帐户时,我在 Android 4.0.2 模拟器中遇到了这个问题。在客户端中设置帐户解决了这个问题。
  • @Sam 不是解释了如何使用Intent 正确发送电子邮件吗?看来下面的答案(您已确认)朝着相同的方向发展。这就是我认为我的链接解决错误的方式。如果没有,那我就不好了。
  • @shkschneider,它确实解释了如何发送电子邮件,但它并没有真正解决“不支持的操作”错误的实际原因。问题中的代码实际上工作正常,但它只是在特定情况下以这种方式中断,从我到目前为止的测试来看,这似乎是以下组合:使用模拟器,使用setData,以及意图不匹配任何东西.这个问题中的代码实际上似乎比 setType 方法更好,因为根据 cmets 在一些 SO 答案中的说法,这种方法会导致 Skype 和其他非电子邮件客户端与意图相匹配。

标签: android email mobile mailto


【解决方案1】:

试试这个,它对我有用:

public void addEmail() {

     TextView txt = (TextView) findViewById(R.id.emailtext);

     txt.setOnClickListener(new View.OnClickListener(){

     public void onClick(View v){

            String[] emails = {"youremail@gmail.com"};
            String subject = "your subject";
            String message = "your message";

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, emails);
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);

            // need this to prompts email client only
            email.setType("message/rfc822");

            startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }});

}

【讨论】:

  • 我已经测试并确认这是可行的。看起来问题是由在没有应用匹配意图时使用 setData() 引起的。
【解决方案2】:

问题可能是您在其中一个官方 Android 模拟器上运行,而您尚未在其上设置电子邮件帐户。发生这种情况时,模拟器会打开 com.android.fallback.Fallback 活动,但在现实世界的设备上似乎不会发生这种情况。

您可以在尝试使用此代码启动意图之前检测到这一点:

ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);

【讨论】:

    【解决方案3】:

    问题是因为您没有在模拟器上设置电子邮件帐户。我在模拟器上遇到了同样的问题,但在手机上测试时就没有了。

    【讨论】:

    • 这个答案只是重申了其他答案和 cmets 中已经说明的内容。
    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 2021-10-01
    • 2021-12-23
    • 1970-01-01
    • 2016-07-28
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多