android Sending Content to Other Apps[ 发送分享的数据到其他App]

1.Send Text Content [分享文本数据]

最常用的是通过ACTION_SEND从一个Activity发送文本内容到另外一个Activity。一段示例Code:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
但一般为了能总是显示 选择界面对话框 和 给选择对话框指定标题,需要为intent调用Intent.createChooser()

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

  • 效果图如下:

2.Send Binary Content  [分享二进制数据]

分享二进制数据需要结合设置适当的MIME Type,需要在EXTRA_STREAM里面放置数据的URI,下面是个分享图片的例子,这个例子也可以修改用来分享任何类型的二进制数据:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));


3.Send Multiple Pieces of Content [分享多条数据内容]

分享多种不同类型的数据内容,需要使用ACTION_SEND_MULTIPLE和提供数据的URIs列表。MIME类型会根据你分享的混合内容而不同。例如,如果你分享3张JPEG格式的图片,那么MIME类型仍然是“image/jpeg”。如果是不同图片格式的话,应该是用“image/*”来匹配那些可以接收任何图片类型的activity。如果你需要分享多种不同类型的数据,可以使用“*/*”来表示MIME。像前面描述的那样,这取决于那些接收的程序解析并处理你的数据。一个例子:

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
注意:请确保提供的URIs能够被接收程序所访问(访问权限的问题)。
详细官方文档:http://developer.android.com/training/sharing/send.html#send-text-content


以下是使用QQ邮箱发送邮件的Android代码示例: 首先,需要在你的Android项目中添加JavaMail和javax.mail(可以在build.gradle中添加): ``` implementation 'com.sun.mail:android-mail:1.6.0' implementation 'com.sun.mail:android-activation:1.6.0' ``` 然后,在你的Activity或者Fragment中创建一个发送邮件的方法: ```java private void sendEmail() { // 收件人列表 String[] to = {"recipient@example.com"}; // 发件人 final String from = "your_qq_email@qq.com"; // 发件人密码 final String password = "your_qq_email_password"; // 配置邮件服务器信息 Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 获取会话 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建邮件对象 Message message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 InternetAddress[] addresses = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { addresses[i] = new InternetAddress(to[i]); } message.setRecipients(Message.RecipientType.TO, addresses); // 设置主题 message.setSubject("Test Email"); // 设置邮件内容 message.setText("This is a test email sent from my Android app."); // 发送邮件 Transport.send(message); Toast.makeText(this, "Email sent successfully!", Toast.LENGTH_SHORT).show(); } catch (MessagingException e) { Toast.makeText(this, "Email sending failed: " + e.getMessage(), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } ``` 以上代码中,需要替换以下内容: - `to`: 收件人的邮箱地址; - `from`: 发件人的QQ邮箱地址; - `password`: 发件人的QQ邮箱密码。 如果你使用的是QQ邮箱,则需要使用SMTP服务器 `smtp.qq.com`,端口号为 `465`。在配置 `props` 对象时,需要使用 SSL socket factory 来建立安全连接。 最后,你可以在你的应用程序中调用 `sendEmail()` 方法来发送邮件。请确保在发送邮件之前,你已经在AndroidManifest.xml中添加了以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值