Android中簡訊傳送與接受

2021-07-15 09:24:10 字數 2500 閱讀 6680

簡訊的傳送

兩種傳送方式:

一、利用系統的簡訊傳送程式

1.intent     傳送乙個隱式意圖

intent    intent=new  intent(intent.action_sendto);

uri   data=uri.parse("smsto:"+"號碼:例如15555215556");

intent.setdata(data);

intent.putextra("sms_body",etcontent.gettext().tostring());

staractivity(intent);

2.另外一種隱式意圖

intent   intent =new   intent(intent.action_view);

intent.settype('address","15555215556");

intent.putextra("sms_body",etcontent.getext().tostring());//獲得的傳送的資訊

startactivity(intent);

二、利用**來傳送簡訊訊息

1.簡單一些的寫法

smsmanager   manager=smsmanager.getdefault();

manager.sentextmessage(對方手機號碼,null,簡訊正文,null,null);

新增上兩個pendingintent,可以獲得簡訊的傳送狀態

//傳送資訊時間

intent   intent=new   intent("com.tarena.sent");

pendingintent   pi1=pendingintent.getbroadcast(this, 0, intent, 0);

//資訊到達對方手機時間

intent   intent2=new intent("com.tarena.delivery");

pendingintent   pi2=pendingintent.getbroadcast(this,0, intent2, 0);

利用**傳送簡訊,有兩點要注意:

1)  申請許可權send_sms

2)  利用**傳送簡訊,簡訊是不會被系統寫入資料表

簡訊的接收

安卓系統後台有乙個服務(service),專門用來接收簡訊訊息,當有新的訊息到達時,service會傳送乙個廣播,廣播 的action是

「android.provider.telephony.sms_received」並且將收到短訊息作為廣播intent的一部分(intent的extra)傳送出去,先到先得!

步驟:1.註冊廣播接收器,接收「android.provider.telephony.sms_received」廣播

在onresume中註冊:重寫方法和onpause配套使用

myreceiver   receiver=new   myreceiver();//自己建立的乙個類,繼承broadcastreceiver

intentfilter    filter=new   intentfilter();

filter.addaction("com.tarena.sent");

filter.addaction("com.tarena.delivery");

filter.addaction("android.provider.telephony.sms_received");

filter.setpriority(1001);

registerreceiver(receiver, filter);

在myreceive中:

if("android.provider.telephony.sms_received".equals(action)){

//可以處理短訊息

bundle bundle=intent.getextras();

object pdus=(object) bundle.get("pdus");

stringbuilder sb=new stringbuilder();

string phonenumber="";

//把乙個乙個pdus轉為一段的短訊息

for (int i = 0; i < pdus.length; i++) {

smsmessage message=smsmessage.createfrompdu((byte)pdus[i]);

在onpause()中登出:

unregisterreceive

2.必須設定許可權receive_sms

3.設定較高的優先順序(1000左右)

filter.setpriority(1001);

4.如果不希望系統的簡訊程式接收簡訊,就執行abortbroadcast();,廣播停止下發

問題:如果系統簡訊接收程式收不到簡訊了,那麼這條簡訊也不會被寫入資料庫

Android中簡訊傳送 介面跳轉

第一次寫技術方面的東西,有點小緊張。下面把我學習android之後,嘗試寫一些簡單的東西拿出來分享一下。傳送簡訊實際是呼叫系統的api來完成的,smsmanager類不能new,通過呼叫其靜態方法getdefault 獲得物件來完成訊息的傳送 public void onclick view v e...

android傳送與接收超長簡訊

android接收傳送簡訊,支援的最大字元數是70個,實際是67個字元,如果傳送的簡訊超過了該數目,那就實現方法不一樣了。接收長簡訊 傳送長簡訊 利用smsmanager的 public void sendmultiparttextmessage string destinationaddress,...

達內課程 Android中簡訊攔截簡訊

攔截簡訊的廣播接收器 class receivesmsreceiver extends broadcastreceiver 如果簡訊內容包含 或者號碼為 5556 的簡訊就會被攔截 可以看到,第一條 hi 和第三條 hello 都能正常接收,第二條 11 被攔截了 同時我們看一下列印的日誌 攔截簡訊...