Android中使用Service实现后台发送邮件功能实例
作者:马到成功 时间:2022-01-26 16:56:05
本文实例讲述了Android中使用Service实现后台发送邮件功能。分享给大家供大家参考,具体如下:
程序如下:
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.widget.TextView;
public class A05Activity extends Activity {
private TextView tv;
private String[] receiver;
private String subject;
private String body;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
tv.setText("等待接收邮件中···");
try {
//取得短信传来的Bundle
Bundle b=this.getIntent().getExtras();
if(b!=null){
//将Bundle中的字符串取出
String s=b.getString("input");
receiver=new String[]{"1650967185@163.com"};
subject="邮箱发送";
body=s.toString();
//自定义一个Intent业执行发送E-mail的工作
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("plain/text");//设置邮件格式为“plain/text”
i.putExtra(android.content.Intent.EXTRA_EMAIL,receiver);//传入收件人地址
i.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);//传入邮件主题
i.putExtra(android.content.Intent.EXTRA_TEXT, body);//传入邮件内容
startActivity(Intent.createChooser(i, getResources().getString(R.string.message)));
}
else{
finish();
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;//用来收取短信
import android.widget.Toast;//告知用户收到短信
@SuppressWarnings("deprecation")
public class ServiceA05 extends BroadcastReceiver{
public static final String mAction="android.provider.Telephony.SMS_RECEIVED";
private String str_receiver="收到短信";
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(arg0, str_receiver.toString(), Toast.LENGTH_LONG).show();
if(arg1.getAction().equals(mAction)){
//建构一字符串集合变量sb
StringBuilder sb=new StringBuilder();
//接收数据
Bundle b=arg1.getExtras();
//判断intent传送数据是否为空
if(b!=null){
//pdus为android内置的短信参数indentifier
/*
* 通过bundle.get("")返回一个包含pdus的对象*/
Object[] myOBJpuds=(Object[])b.get("pdus");
//构造短信对象数组,并根据短信内容大小来确定数组的大小
SmsMessage[] sm=new SmsMessage[myOBJpuds.length];
for(int i=0;i<myOBJpuds.length;i++){
sm[i]=SmsMessage.createFromPdu((byte[])myOBJpuds[i]);
}
//将短信合并自定义信息于StringBuilder当中
for(SmsMessage sm01:sm){
sb.append("接收到来自:\n");
//收信人的电话号码
sb.append(sm01.getDisplayOriginatingAddress());
sb.append("\n--------传来的短信---------\n");
//取得传来短信的内容
sb.append(sm01.getDisplayMessageBody());
//用Toast来显示来电信息
Toast.makeText(arg0, sb.toString(), Toast.LENGTH_LONG).show();
}
}
Toast.makeText(arg0, sb.toString(), Toast.LENGTH_LONG).show();
//返回主Activity
Intent i=new Intent(arg0,A05Activity.class);
//定义一个Bundle
Bundle b01=new Bundle();
//将短信以putString()方法存入Bundle中
b01.putString("input", sb.toString());
//将Bundle放入Intent中
i.putExtras(b01);
//设置Intent的Flag以一个全新的task来运行
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);
}
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.a05"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".A05Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ServiceA05">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
在android中注册一个BroadcastReceiver,并设置这个receiver的intent-filter(Android.provider.Telephony.SMS_RECEIVED),让它针对短信事件做出反应。并且还要添加一个权限:android.permission.RECEIVE_SMS。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,Service,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解关于Android Studio中安装和gradle的一些坑
2023-06-10 05:58:30
![](https://img.aspxhome.com/file/2023/8/120048_0s.png)
深入学习java位运算的基础知识
2021-06-01 07:10:40
Springboot集成spring data elasticsearch过程详解
2023-04-16 00:42:16
![](https://img.aspxhome.com/file/2023/4/70484_0s.png)
java编程创建型设计模式单例模式的七种示例
2023-06-21 08:47:39
![](https://img.aspxhome.com/file/2023/7/77387_0s.png)
Android Studio进行APP图标更改的两种方式总结
2022-02-07 09:08:40
![](https://img.aspxhome.com/file/2023/7/111747_0s.png)
解决SpringMVC Controller 接收页面传递的中文参数出现乱码的问题
2021-08-06 20:52:42
![](https://img.aspxhome.com/file/2023/9/86709_0s.jpg)
解决@PathVariable对于特殊字符截断的问题
2021-10-10 08:19:40
C#将DataTable转换成list的方法
2023-03-06 01:20:45
解析Android开发中多点触摸的实现方法
2021-12-07 11:20:35
Android 钱包支付之输入支付密码的实现步骤
2021-09-23 11:17:09
![](https://img.aspxhome.com/file/2023/7/137457_0s.png)
通过Java实现bash命令过程解析
2023-01-07 17:38:25
java数据结构之希尔排序
2023-11-08 18:16:18
一文带你了解SpringBoot的启动原理
2023-11-28 20:44:42
C# List 并发丢数据问题原因及解决方案
2023-06-18 17:53:54
c# 如何实现一个简单的json解析器
2021-06-10 15:02:54
![](https://img.aspxhome.com/file/2023/9/113719_0s.png)
Java 注解学习笔记
2022-12-25 02:40:54
C#判断日期是否到期的方法
2021-09-10 14:40:00
Java实现双保险线程的示例代码
2023-09-21 07:59:46
Android中使用itemdecoration实现时间线效果
2021-10-08 12:15:49
![](https://img.aspxhome.com/file/2023/5/100615_0s.png)
VS2019编写C程序或者CUDA程序出现“无法启动程序,系统找不到指定的文件”问题的详细解决方法
2022-04-25 08:09:31
![](https://img.aspxhome.com/file/2023/4/131954_0s.png)