举例说明如何在android中利用Service来实现后台发送邮件功能
2012-07-30 21:02
916 查看
程序如下:
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。
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实现后台发送邮件功能实例
- android利用javamail实现后台发送可添加附件的邮件
- Android: 如何利用Handler处理和发送消息来实现页面刷新的功能
- Android service学习总结和利用service实现的后台音乐播放功能
- 举例说明如何在android中实现设置黑名单的功能--AudioManager、PhoneStateListener的使用
- 举例说明android中实现发送邮件的方法
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- Android: 如何利用Handler处理和发送消息来实现页面刷新的功能
- 利用mutt和msmtp实现邮件自动发送功能
- Android实现拨打电话、发送短信和发送邮件功能
- 利用Node.JS实现邮件发送功能
- 深入剖析Android四大组件(六)——相对完美的后台Service实现播放音乐功能
- 利用CDO实现邮件阅读或发送回执功能 For .Net
- 利用mutt和msmtp实现邮件自动发送功能
- Android后台发送邮件实现用户反馈
- 利用邮件对象实现发送QQ日志以及检测用户是否开通SMTP功能
- android实现后台自动发邮件功能
- Android开发之发送邮件功能的实现(源码分享)
- 举例说明BroadcastReceiver的使用方法----实现android手机开机之后显示画面的功能