欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中使用Service實現(xiàn)后臺發(fā)送郵件功能實例

 更新時間:2016年01月29日 11:34:50   作者:馬到成功  
這篇文章主要介紹了Android中使用Service實現(xiàn)后臺發(fā)送郵件功能的方法,結合實例形式分析了Service實現(xiàn)郵件的發(fā)送、接收及權限控制相關技巧,需要的朋友可以參考下

本文實例講述了Android中使用Service實現(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");//設置郵件格式為“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();
  //接收數(shù)據(jù)
  Bundle b=arg1.getExtras();
  //判斷intent傳送數(shù)據(jù)是否為空
  if(b!=null){
  //pdus為android內置的短信參數(shù)indentifier
  /*
   * 通過bundle.get("")返回一個包含pdus的對象*/
  Object[] myOBJpuds=(Object[])b.get("pdus");
  //構造短信對象數(shù)組,并根據(jù)短信內容大小來確定數(shù)組的大小
  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開發(fā)入門與進階教程

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

相關文章

  • android實現(xiàn)撲克卡片翻轉

    android實現(xiàn)撲克卡片翻轉

    這篇文章主要為大家詳細介紹了android實現(xiàn)撲克卡片翻轉,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Flutter?WebView?預加載實現(xiàn)方法(Http?Server)

    Flutter?WebView?預加載實現(xiàn)方法(Http?Server)

    這篇文章主要介紹了Flutter?WebView?預加載實現(xiàn)方法,包括資源的配置,資源的下載和存儲,版本的管理,如何根據(jù)實際url獲取對應HttpServer?bind的url等,需要的朋友可以參考下
    2022-05-05
  • Android仿QQ好友詳情頁下拉頂部圖片縮放效果

    Android仿QQ好友詳情頁下拉頂部圖片縮放效果

    這篇文章主要為大家詳細介紹了Android仿QQ好友詳情頁下拉頂部圖片縮放效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • android實現(xiàn)App活動定時自動跳轉效果

    android實現(xiàn)App活動定時自動跳轉效果

    本篇文章主要介紹了android實現(xiàn)App活動定時自動跳轉效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • 修改Android中hosts文件的步驟詳解

    修改Android中hosts文件的步驟詳解

    有朋友問Android怎么修改Hosts?對于這個問題,由于手頭并沒有Android設備,所以只能從網(wǎng)上搜羅了方法并總結出來,下面這篇文章主要介紹了修改Android中hosts文件的步驟,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • Android studio 生成帶Kotlin文檔的實現(xiàn)方式

    Android studio 生成帶Kotlin文檔的實現(xiàn)方式

    這篇文章主要介紹了Android studio 生成帶Kotlin文檔的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android實現(xiàn)圖片拖拉功能

    Android實現(xiàn)圖片拖拉功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片拖拉功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Flutter?文字中劃線動畫StrikeThroughTextAnimation

    Flutter?文字中劃線動畫StrikeThroughTextAnimation

    這篇文章主要為大家介紹了Flutter?文字中劃線動畫StrikeThroughTextAnimation示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Android入門之Service的使用詳解

    Android入門之Service的使用詳解

    我們的Android在啟動一些長事務時都會使用異步,很多初學者覺得這個異步就是一個異步線程+Handler而己。如果你這么想就錯了。這一切其實靠的正是Android里的Service。本文就來和大家聊聊Service的生命周期和使用,需要的可以參考一下
    2022-12-12
  • flutter?material?widget組件之信息展示組件使用詳解

    flutter?material?widget組件之信息展示組件使用詳解

    這篇文章主要為大家詳細介紹了flutter?material?widget組件之信息展示組件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論