Android中使用Service實(shí)現(xiàn)后臺發(fā)送郵件功能實(shí)例
本文實(shí)例講述了Android中使用Service實(shí)現(xiàn)后臺發(fā)送郵件功能。分享給大家供大家參考,具體如下:
程序如下:
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="郵箱發(fā)送";
body=s.toString();
//自定義一個Intent業(yè)執(zhí)行發(fā)送E-mail的工作
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("plain/text");//設(shè)置郵件格式為“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);//傳入郵件內(nèi)容
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)){
//建構(gòu)一字符串集合變量sb
StringBuilder sb=new StringBuilder();
//接收數(shù)據(jù)
Bundle b=arg1.getExtras();
//判斷intent傳送數(shù)據(jù)是否為空
if(b!=null){
//pdus為android內(nèi)置的短信參數(shù)indentifier
/*
* 通過bundle.get("")返回一個包含pdus的對象*/
Object[] myOBJpuds=(Object[])b.get("pdus");
//構(gòu)造短信對象數(shù)組,并根據(jù)短信內(nèi)容大小來確定數(shù)組的大小
SmsMessage[] sm=new SmsMessage[myOBJpuds.length];
for(int i=0;i<myOBJpuds.length;i++){
sm[i]=SmsMessage.createFromPdu((byte[])myOBJpuds[i]);
}
//將短信合并自定義信息于StringBuilder當(dāng)中
for(SmsMessage sm01:sm){
sb.append("接收到來自:\n");
//收信人的電話號碼
sb.append(sm01.getDisplayOriginatingAddress());
sb.append("\n--------傳來的短信---------\n");
//取得傳來短信的內(nèi)容
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);
//設(shè)置Intent的Flag以一個全新的task來運(yùn)行
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,并設(shè)置這個receiver的intent-filter(Android.provider.Telephony.SMS_RECEIVED),讓它針對短信事件做出反應(yīng)。并且還要添加一個權(quán)限:android.permission.RECEIVE_SMS。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》及《Android開發(fā)入門與進(jìn)階教程》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android 后臺發(fā)送郵件到指定郵箱
- Android快速實(shí)現(xiàn)發(fā)送郵件實(shí)例
- Android發(fā)送郵件的方法實(shí)例詳解
- Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實(shí)現(xiàn))
- Android 后臺發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
- Android開發(fā)中怎樣調(diào)用系統(tǒng)Email發(fā)送郵件(多種調(diào)用方式)
- android實(shí)現(xiàn)自動發(fā)送郵件
相關(guān)文章
android仿微信表情雨下落效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于android仿微信表情雨下落效果的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android BaseAdapter應(yīng)用實(shí)例
這篇文章主要介紹了Android BaseAdapter應(yīng)用方法,結(jié)合生成聯(lián)系人Items的實(shí)例形式分析了BaseAdapter的使用技巧,需要的朋友可以參考下2016-01-01
Android音樂播放器制作 點(diǎn)擊歌曲實(shí)現(xiàn)播放(二)
這篇文章主要為大家詳細(xì)介紹了Android音樂播放器的制作方法,點(diǎn)擊歌曲實(shí)現(xiàn)播放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android ListView出現(xiàn)異常解決辦法
這篇文章主要介紹了Android ListView出現(xiàn)異常ListView:The content of the adapter has changed but ListView did not receive a notification解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11

