国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中使用Service實現后臺發送郵件功能實例

2020-04-11 10:53:24
字體:
來源:轉載
供稿:網友

本文實例講述了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控件用法總結》及《Android開發入門與進階教程

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海兴县| 杂多县| 崇信县| 嘉黎县| 恩施市| 东丰县| 湘阴县| 同仁县| 岳阳市| 轮台县| 广元市| 会宁县| 龙海市| 广德县| 响水县| 滦南县| 台东县| 和田县| 东明县| 迁西县| 鞍山市| 莱阳市| 嘉峪关市| 开平市| 错那县| 辽阳县| 宁夏| 剑川县| 岫岩| 罗田县| 博兴县| 尤溪县| 北安市| 女性| 绥德县| 永安市| 兴化市| 新巴尔虎右旗| 嵊州市| 青川县| 洛宁县|