我是新手,但我的编码片段有什么问题? 我收到错误:当我选择链接时,“当前不支持此操作”。 这是我的代码:
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"));
}});
}
非常感谢!
答案 0 :(得分:14)
问题可能是你在其中一个官方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);
答案 1 :(得分: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 :"));
}});
}
答案 2 :(得分:-1)
问题是因为您尚未在模拟器上设置电子邮件帐户。我在模拟器上遇到了同样的问题,但在手机上测试时却没有。