Android如何實現(xiàn)接收和發(fā)送短信
每一部手機都具有短信接收和發(fā)送功能,下面我們通過代碼來實現(xiàn)接收和發(fā)送短信功能。
一、接收短信
1、創(chuàng)建內(nèi)部廣播接收器類,接收系統(tǒng)發(fā)出的短信廣播
2、從獲得的內(nèi)容中解析出短信發(fā)送者和短信內(nèi)容
3、在Activity中注冊廣播
4、添加接收短信權限
下面放上具體的代碼
activity_main.xml文件用于顯示短信發(fā)送者號碼和顯示短信內(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(); //設置較高的優(yōu)先級 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ā)送方號碼 String address = messages[0].getOriginatingAddress(); String fullMessage = ""; for (SmsMessage message : messages) { //獲取短信內(nèi)容 fullMessage += message.getMessageBody(); } //截斷廣播,阻止其繼續(xù)被Android自帶的短信程序接收到 abortBroadcast(); fromTv.setText(address); contentTv.setText(fullMessage); } } }
注:注冊的廣播接收器,一定要在OnDestroy()方法中取消注冊。
由于短信廣播是有序廣播,如果我們不想讓Android自帶的短信程序接收到短信,就可以設置我們自身接收器的優(yōu)先級,同時在我們接受完廣播后將廣播截斷,阻止其被Android自帶的短信程序接收到。
二、發(fā)送短信
1、獲取接收者的號碼和短信內(nèi)容
2、獲得短信發(fā)送管理實例
3、構(gòu)造PendingIntent啟動短信發(fā)送狀態(tài)監(jiān)控廣播
4、調(diào)用發(fā)送短信函數(shù),傳入?yún)?shù)發(fā)送短信
5、構(gòu)造廣播接收器內(nèi)部類監(jiān)控短信是否發(fā)送成功
6、獲得廣播接收器實例和IntentFilter實例,注冊廣播接收器
7、在onDestroy()中取消注冊的廣播接收器
8、在AndroidManifest.xml文件中加入短信發(fā)送權限
下面放上具體的布局文件和代碼
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ā)送按鈕的點擊監(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() { //獲取短信接收者號碼 String to = toEdit.getText().toString(); //獲取發(fā)送短信內(nèi)容 String content = toContent.getText().toString(); //獲得廣播接收器實例和IntentFilter實例 sendStatusReceiver = new SendStatusReceiver(); sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); //注冊廣播監(jiān)聽 registerReceiver(sendStatusReceiver, sendFilter); //構(gòu)造PendingIntent啟動短信發(fā)送狀態(tài)監(jiān)控廣播 Intent sendIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast(context, 0, sendIntent, 0); //獲得短信管理實例 SmsManager smsManager = SmsManager.getDefault(); //調(diào)用發(fā)送短信函數(shù),傳入?yún)?shù)發(fā)送短信(第一、三、四參數(shù)依次為接收者號碼,短信內(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(); //取消注冊的廣播 unregisterReceiver(sendStatusReceiver); } }
在AndroidManifest.xml文件中加入短信發(fā)送權限
<uses-permission android:name="android.permission.SEND_SMS"/>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)工程中集成mob短信驗證碼功能的方法
- Android手機號注冊、綁定手機號獲取短信驗證碼實例
- Android獲取和讀取短信驗證碼的實現(xiàn)方法
- Android實現(xiàn)自動提取短信驗證碼功能
- Android實現(xiàn)短信驗證碼自動填寫功能
- Android黑科技之讀取用戶短信+修改系統(tǒng)短信數(shù)據(jù)庫
- android短信攔截的實現(xiàn)代碼
- android教程之intent的action屬性使用示例(intent發(fā)短信)
- 獲取Android手機中所有短信的實現(xiàn)代碼
- Android短信接收監(jiān)聽、自動回復短信操作例子
相關文章
Android實現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地
這篇文章主要為大家詳細介紹了Android實現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Android列表實現(xiàn)(3)_自定義列表適配器思路及實現(xiàn)代碼
Android 自定義列表適配器會提供很多的便利;下面的例子為使用自定義的列表適配器來顯示列表,感興趣的朋友可以研究下2012-12-12Android自定義Chronometer實現(xiàn)短信驗證碼秒表倒計時功能
這篇文章主要介紹了Android自定義ChronometerView實現(xiàn)類似秒表倒計時,短信驗證碼倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11android為ListView每個Item上面的按鈕添加事件
本篇文章主要介紹了android為ListView每個Item上面的按鈕添加事件,有興趣的同學可以了解一下。2016-11-11