Android如何實(shí)現(xiàn)接收和發(fā)送短信
每一部手機(jī)都具有短信接收和發(fā)送功能,下面我們通過代碼來實(shí)現(xiàn)接收和發(fā)送短信功能。
一、接收短信
1、創(chuàng)建內(nèi)部廣播接收器類,接收系統(tǒng)發(fā)出的短信廣播
2、從獲得的內(nèi)容中解析出短信發(fā)送者和短信內(nèi)容
3、在Activity中注冊(cè)廣播
4、添加接收短信權(quán)限
下面放上具體的代碼
activity_main.xml文件用于顯示短信發(fā)送者號(hào)碼和顯示短信內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/sms_from" android:layout_width="wrap_content" android:layout_height="20dp" android:text="From" /> <TextView android:id="@+id/sms_from_txt" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/sms_from"/> <TextView android:id="@+id/sms_content" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_below="@id/sms_from" android:layout_marginTop="20dp" android:text="Content" /> <TextView android:id="@+id/sms_content_txt" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="15dp" android:layout_marginTop="20dp" android:layout_below="@id/sms_from_txt" android:layout_toRightOf="@id/sms_content"/> </RelativeLayout>
MainActivity.java文件
public class MainActivity extends AppCompatActivity { private TextView fromTv; private TextView contentTv; private IntentFilter intentFilter; private MessageReceiver messageReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getSms(); } private void getSms() { intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); messageReceiver = new MessageReceiver(); //設(shè)置較高的優(yōu)先級(jí) intentFilter.setPriority(100); registerReceiver(messageReceiver, intentFilter); } private void initView() { fromTv = (TextView) findViewById(R.id.sms_from_txt); contentTv = (TextView) findViewById(R.id.sms_content_txt); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver); } class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); //提取短信消息 Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } //獲取發(fā)送方號(hào)碼 String address = messages[0].getOriginatingAddress(); String fullMessage = ""; for (SmsMessage message : messages) { //獲取短信內(nèi)容 fullMessage += message.getMessageBody(); } //截?cái)鄰V播,阻止其繼續(xù)被Android自帶的短信程序接收到 abortBroadcast(); fromTv.setText(address); contentTv.setText(fullMessage); } } }
注:注冊(cè)的廣播接收器,一定要在OnDestroy()方法中取消注冊(cè)。
由于短信廣播是有序廣播,如果我們不想讓Android自帶的短信程序接收到短信,就可以設(shè)置我們自身接收器的優(yōu)先級(jí),同時(shí)在我們接受完廣播后將廣播截?cái)?,阻止其被Android自帶的短信程序接收到。
二、發(fā)送短信
1、獲取接收者的號(hào)碼和短信內(nèi)容
2、獲得短信發(fā)送管理實(shí)例
3、構(gòu)造PendingIntent啟動(dòng)短信發(fā)送狀態(tài)監(jiān)控廣播
4、調(diào)用發(fā)送短信函數(shù),傳入?yún)?shù)發(fā)送短信
5、構(gòu)造廣播接收器內(nèi)部類監(jiān)控短信是否發(fā)送成功
6、獲得廣播接收器實(shí)例和IntentFilter實(shí)例,注冊(cè)廣播接收器
7、在onDestroy()中取消注冊(cè)的廣播接收器
8、在AndroidManifest.xml文件中加入短信發(fā)送權(quán)限
下面放上具體的布局文件和代碼
activity_send_msg.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/to_ed" android:layout_width="match_parent" android:layout_height="50dp" android:hint="to"/> <EditText android:id="@+id/to_content" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/to_ed" android:hint="content"/> <Button android:id="@+id/send_msg" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/to_content" android:text="@string/send_message"/> </RelativeLayout>
SendMsgActivity.java文件
public class SendMsgActivity extends AppCompatActivity implements View.OnClickListener { private Context context; private EditText toEdit; private EditText toContent; private IntentFilter sendFilter; private SendStatusReceiver sendStatusReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_msg); context = this; initView(); } private void initView() { toEdit = (EditText) findViewById(R.id.to_ed); toContent = (EditText) findViewById(R.id.to_content); //添加發(fā)送按鈕的點(diǎn)擊監(jiān)聽事件 Button sendMsg = (Button) findViewById(R.id.send_msg); sendMsg.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.send_msg: sendMessage(); break; default: break; } } private void sendMessage() { //獲取短信接收者號(hào)碼 String to = toEdit.getText().toString(); //獲取發(fā)送短信內(nèi)容 String content = toContent.getText().toString(); //獲得廣播接收器實(shí)例和IntentFilter實(shí)例 sendStatusReceiver = new SendStatusReceiver(); sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); //注冊(cè)廣播監(jiān)聽 registerReceiver(sendStatusReceiver, sendFilter); //構(gòu)造PendingIntent啟動(dòng)短信發(fā)送狀態(tài)監(jiān)控廣播 Intent sendIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast(context, 0, sendIntent, 0); //獲得短信管理實(shí)例 SmsManager smsManager = SmsManager.getDefault(); //調(diào)用發(fā)送短信函數(shù),傳入?yún)?shù)發(fā)送短信(第一、三、四參數(shù)依次為接收者號(hào)碼,短信內(nèi)容,短信發(fā)送狀態(tài)監(jiān)控的PendingIntent) smsManager.sendTextMessage(to, null, content, pi, null); } /** * 構(gòu)造廣播接收器內(nèi)部類監(jiān)控短信是否發(fā)送成功 */ class SendStatusReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (getResultCode() == RESULT_OK){ Toast.makeText(context, "successful", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context, "failed", Toast.LENGTH_SHORT).show(); } } } @Override protected void onDestroy() { super.onDestroy(); //取消注冊(cè)的廣播 unregisterReceiver(sendStatusReceiver); } }
在AndroidManifest.xml文件中加入短信發(fā)送權(quán)限
<uses-permission android:name="android.permission.SEND_SMS"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)工程中集成mob短信驗(yàn)證碼功能的方法
- Android手機(jī)號(hào)注冊(cè)、綁定手機(jī)號(hào)獲取短信驗(yàn)證碼實(shí)例
- Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)自動(dòng)提取短信驗(yàn)證碼功能
- Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能
- Android黑科技之讀取用戶短信+修改系統(tǒng)短信數(shù)據(jù)庫
- android短信攔截的實(shí)現(xiàn)代碼
- android教程之intent的action屬性使用示例(intent發(fā)短信)
- 獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼
- Android短信接收監(jiān)聽、自動(dòng)回復(fù)短信操作例子
相關(guān)文章
Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android列表實(shí)現(xiàn)(3)_自定義列表適配器思路及實(shí)現(xiàn)代碼
Android 自定義列表適配器會(huì)提供很多的便利;下面的例子為使用自定義的列表適配器來顯示列表,感興趣的朋友可以研究下2012-12-12Flutter質(zhì)感設(shè)計(jì)之列表項(xiàng)
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之列表項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
這篇文章主要介紹了Android自定義ChronometerView實(shí)現(xiàn)類似秒表倒計(jì)時(shí),短信驗(yàn)證碼倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件
本篇文章主要介紹了android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。2016-11-11快速關(guān)閉android studio的自動(dòng)保存功能教程
這篇文章主要介紹了快速關(guān)閉android studio的自動(dòng)保存功能教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Android入門教程之ListView的應(yīng)用示例
這篇文章主要介紹了Android入門教程之ListView的應(yīng)用,結(jié)合簡(jiǎn)單實(shí)例形式分析了Android中l(wèi)istview的簡(jiǎn)單創(chuàng)建與使用步驟,需要的朋友可以參考下2016-10-10