intent.ACTION_SEND 发送邮件或者任何内容到邮件客户端或者qq 微信什么的

本文详细介绍了如何使用Intent在Android系统中发送邮件,并解释了应用过滤机制如何选择合适的邮件客户端。通过设置Intent的Action、MIME type和Extra参数,可以确保邮件正确发送,并在系统中列出所有能处理此类邮件的程序。
  1. Intent sendIntent = new Intent();  
  2. sendIntent.setAction(Intent.ACTION_SEND);  
  3. //这里是你发送的文本 
  4. sendIntent.putExtra(Intent.EXTRA_EMAIL, recipients);
  5. sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
    sendIntent.putExtra(Intent.EXTRA_TEXT, body.getText().toString());

  6. sendIntent.putExtra(Intent.EXTRA_TEXT, "我用手机测试分享内容");  
  7. sendIntent.setType("text/plain");  
  8. startActivity(sendIntent);


假如你安装了过滤Action为ACTION_SEND,MIMEtype为“text/plain”的应用程序,这个系统就会启动,假如系统匹配到多个这样子的应用程序,他就会弹出一个Dialog列出所有的应用供用户选择


实际测试htcone 5.1

如果不加setType,则提示找不到处理此事件的程序

http://www.tutorialspoint.com/android/android_intents_filters.htm    中例子就没有




加了后会列出一大堆,貌似只要是能发送信息的软件全列了出来



但是在模拟器中自动就调到发送信息界面

但是这个界面的SurveyActivity.java:package com.example.bus; import android.os.Bundle; import android.widget.*; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.app.AlertDialog; public class SurveyActivity extends AppCompatActivity { private static final String FEEDBACK_EMAIL = "2689724318@qq.com"; // 修改为你自己的邮箱地址 private static final String EMAIL_SUBJECT = "用户调研反馈"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_survey); // 显示 ActionBar 上的返回箭头 if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("用户调研"); } // 绑定控件 RadioGroup radioGroupRating = findViewById(R.id.radioGroupRating); CheckBox cbUi = findViewById(R.id.cb_ui); CheckBox cbSpeed = findViewById(R.id.cb_speed); CheckBox cbInformation = findViewById(R.id.cb_information); CheckBox cbOther = findViewById(R.id.cb_other); EditText etSuggestion = findViewById(R.id.et_suggestion); Button btnSubmit = findViewById(R.id.btn_submit); // 提交按钮点击事件 btnSubmit.setOnClickListener(v -> { // 获取评分 int selectedId = radioGroupRating.getCheckedRadioButtonId(); String rating = "未评分"; if (selectedId == R.id.rb1) rating = "1分"; else if (selectedId == R.id.rb2) rating = "2分"; else if (selectedId == R.id.rb3) rating = "3分"; else if (selectedId == R.id.rb4) rating = "4分"; else if (selectedId == R.id.rb5) rating = "5分"; // 获取满意度选项 StringBuilder satisfaction = new StringBuilder(); if (cbUi.isChecked()) satisfaction.append("界面美观易用, "); if (cbSpeed.isChecked()) satisfaction.append("加载速度快, "); if (cbInformation.isChecked()) satisfaction.append("功能丰富实用, "); if (cbOther.isChecked()) satisfaction.append("其他, "); String satisfactionStr = satisfaction.length() > 0 ? satisfaction.substring(0, satisfaction.length() - 2) : "无"; // 获取建议 String suggestion = etSuggestion.getText().toString().trim(); if (suggestion.isEmpty()) suggestion = "无"; // 拼接邮件正文 String body = String.format( "用户调研反馈\n\n" + "【整体评分】\n%s\n\n" + "【满意项】\n%s\n\n" + "【改进建议】\n%s\n\n" + "---\n来自 Android 客户端自动提交", rating, satisfactionStr, suggestion ); // 发送邮件 sendEmail(body); }); } /** * 发送邮件核心方法 */ private void sendEmail(String body) { try { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); // 纯文本 // 设置收件人邮箱(必须为字符串数组) intent.putExtra(Intent.EXTRA_EMAIL, new String[]{FEEDBACK_EMAIL}); // 设置主题和正文 intent.putExtra(Intent.EXTRA_SUBJECT, EMAIL_SUBJECT); intent.putExtra(Intent.EXTRA_TEXT, body); // 启动系统选择器 startActivity(Intent.createChooser(intent, "选择邮件客户端")); } catch (Exception e) { Toast.makeText(this, "无法启动邮件应用,请安装Gmail、QQ邮箱等程序", Toast.LENGTH_LONG).show(); } } @Override public boolean onSupportNavigateUp() { onBackPressed(); // 执行返回操作 return true; // 表示已处理该事件 } } 为什么我调试的过程中,点击提交反馈,我的QQ邮箱没收到反馈记录呢
11-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值