Android如何實(shí)現(xiàn)接收和發(fā)送短信
每一部手機(jī)都具有短信接收和發(fā)送功能,下面我們通過(guò)代碼來(lái)實(shí)現(xiàn)接收和發(fā)送短信功能。
一、接收短信
1、創(chuàng)建內(nèi)部廣播接收器類(lèi),接收系統(tǒng)發(fā)出的短信廣播
2、從獲得的內(nèi)容中解析出短信發(fā)送者和短信內(nèi)容
3、在A(yíng)ctivity中注冊(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)部類(lèi)監(jiān)控短信是否發(fā)送成功
6、獲得廣播接收器實(shí)例和IntentFilter實(shí)例,注冊(cè)廣播接收器
7、在onDestroy()中取消注冊(cè)的廣播接收器
8、在A(yíng)ndroidManifest.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)聽(tīng)事件
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)聽(tīng)
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)部類(lè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);
}
}
在A(yíng)ndroidManifest.xml文件中加入短信發(fā)送權(quán)限
<uses-permission android:name="android.permission.SEND_SMS"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(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)填寫(xiě)功能
- Android黑科技之讀取用戶(hù)短信+修改系統(tǒng)短信數(shù)據(jù)庫(kù)
- android短信攔截的實(shí)現(xiàn)代碼
- android教程之intent的action屬性使用示例(intent發(fā)短信)
- 獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼
- Android短信接收監(jiān)聽(tīng)、自動(dòng)回復(fù)短信操作例子
相關(guān)文章
Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式,四種實(shí)現(xiàn)網(wǎng)絡(luò)通信的方式供大家學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-06-06
Android積分簽到上移消失動(dòng)畫(huà)效果
這篇文章主要介紹了Android積分簽到上移消失動(dòng)畫(huà)效果大致思路:動(dòng)畫(huà)部分,由一個(gè)垂直的平移和一個(gè)透明度變化的兩個(gè)動(dòng)畫(huà)組成。然后通過(guò)AnimationSet將兩個(gè)動(dòng)畫(huà)添加到集合,然后開(kāi)始播放動(dòng)畫(huà)。2018-05-05
Android 點(diǎn)擊屏幕空白處收起輸入法軟鍵盤(pán)(手動(dòng)打開(kāi))
很多時(shí)候,我們?cè)谑褂脩?yīng)用時(shí),會(huì)出現(xiàn)輸入法軟鍵盤(pán)彈出的問(wèn)題,通常情況下,我們默認(rèn)會(huì)使用戶(hù)點(diǎn)擊返回鍵或者下一步對(duì)軟鍵盤(pán)進(jìn)行隱藏。為了更好的體驗(yàn),我們可以實(shí)現(xiàn)當(dāng)用戶(hù)使用完畢軟鍵盤(pán)時(shí)。點(diǎn)擊屏幕空白即可實(shí)現(xiàn)收起輸入法軟鍵盤(pán)2016-12-12
Android使用google breakpad捕獲分析native cash
這篇文章主要介紹了Android使用google breakpad捕獲分析native cash 的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Android設(shè)置透明狀態(tài)欄和透明導(dǎo)航欄
本文主要介紹了Android設(shè)置透明狀態(tài)欄和透明導(dǎo)航欄的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android中自定義水平進(jìn)度條樣式之黑色虛線(xiàn)
這篇文章主要介紹了Android中自定義水平進(jìn)度條樣式之黑色虛線(xiàn) 的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android Studio下的APP目錄結(jié)構(gòu)詳解
這篇文章主要介紹了AndroidStudio下的APP目錄結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

