Android接收和發(fā)送短信處理
關(guān)于短信接收處理方面,當(dāng)前已經(jīng)有一些app做的比較好了,比如發(fā)給手機(jī)發(fā)驗(yàn)證碼驗(yàn)證的問(wèn)題,很多app在手機(jī)接收到驗(yàn)證碼后,不需要輸入,就直接可以跳過(guò)驗(yàn)證界面,這就是用到了對(duì)接收到的短信的處理。至于短信的發(fā)送,也沒(méi)什么好說(shuō)的了。在此也只是附上一個(gè)小實(shí)例。
效果圖:

MainActivity:
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView sender;
private TextView content;
private IntentFilter receiveFilter;
private MessageReceiver messageReceiver;
private EditText to;
private EditText msgInput;
private Button send;
private IntentFilter sendFilter;
private SendStatusReceiver sendStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sender = (TextView) findViewById(R.id.sender);
content = (TextView) findViewById(R.id.content);
to = (EditText) findViewById(R.id.to);
msgInput = (EditText) findViewById(R.id.msg_input);
send = (Button) findViewById(R.id.send);
//為接收短信設(shè)置要監(jiān)聽(tīng)的廣播
receiveFilter = new IntentFilter();
receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
messageReceiver = new MessageReceiver();
registerReceiver(messageReceiver, receiveFilter);
//為發(fā)送短信設(shè)置要監(jiān)聽(tīng)的廣播
sendFilter = new IntentFilter();
sendFilter.addAction("SENT_SMS_ACTION");
sendStatusReceiver = new SendStatusReceiver();
registerReceiver(sendStatusReceiver, sendFilter);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//發(fā)送短信
//并使用sendTextMessage的第四個(gè)參數(shù)對(duì)短信的發(fā)送狀態(tài)進(jìn)行監(jiān)控
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent("SENT_SMS_ACTION");
PendingIntent pi = PendingIntent.getBroadcast(
MainActivity.this, 0, sentIntent, 0);
smsManager.sendTextMessage(to.getText().toString(), null,
msgInput.getText().toString(), pi, null);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//在Activity摧毀的時(shí)候停止監(jiān)聽(tīng)
unregisterReceiver(messageReceiver);
unregisterReceiver(sendStatusReceiver);
}
class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
//使用pdu秘鑰來(lái)提取一個(gè)pdus數(shù)組
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();
//獲取短信內(nèi)容
String fullMessage = "";
for (SmsMessage message : messages) {
fullMessage += message.getMessageBody();
}
sender.setText(address);
content.setText(fullMessage);
}
}
class SendStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == RESULT_OK) {
//發(fā)送成功
Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG)
.show();
} else {
//發(fā)送失敗
Toast.makeText(context, "Send failed", Toast.LENGTH_LONG)
.show();
}
}
}
}
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="From:" />
<TextView
android:id="@+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="Content:" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="To:" />
<EditText
android:id="@+id/to"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<EditText
android:id="@+id/msg_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Send" />
</LinearLayout>
</LinearLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
//接受短信
<uses-permission android:name="android.permission.RECEIVE_SMS" />
//發(fā)送短信
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smstest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- android中可以通過(guò)兩種方式調(diào)用接口發(fā)送短信
- Android Mms之:短信發(fā)送流程(圖文詳解)
- Android實(shí)現(xiàn)將已發(fā)送的短信寫(xiě)入短信數(shù)據(jù)庫(kù)的方法
- Android發(fā)送短信功能代碼
- Android短信發(fā)送器實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)讀取手機(jī)聯(lián)系人、撥號(hào)、發(fā)送短信及長(zhǎng)按菜單操作方法實(shí)例小結(jié)
- Android實(shí)現(xiàn)短信發(fā)送功能
- Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- Android基礎(chǔ)開(kāi)發(fā)小案例之短信發(fā)送器
相關(guān)文章
Android 中基于TabLayout+ViewPager實(shí)現(xiàn)標(biāo)簽卡效果
這篇文章主要介紹了Android 中基于TabLayout+ViewPager實(shí)現(xiàn)標(biāo)簽卡效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
使用Jetpack Compose實(shí)現(xiàn)翻轉(zhuǎn)卡片效果流程詳解
Jetpack Compose 是一款基于 Kotlin 的聲明式 UI 工具包,可以方便地創(chuàng)建漂亮的用戶(hù)界面。使用 Compose 的動(dòng)畫(huà) API 和可繪制 API,可以輕松實(shí)現(xiàn)翻轉(zhuǎn)卡片效果。通過(guò)設(shè)置旋轉(zhuǎn)角度和透明度等屬性,可以使卡片沿著 Y 軸翻轉(zhuǎn),并實(shí)現(xiàn)翻頁(yè)效果2023-05-05
Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11
Kotlin對(duì)象的懶加載方式by?lazy?與?lateinit?異同詳解
這篇文章主要為大家介紹了Kotlin對(duì)象的懶加載方式by?lazy?與?lateinit?異同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
android第三方分享方式的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了android第三方分享方式的簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android中實(shí)現(xiàn)WebView和JavaScript的互相調(diào)用詳解
這篇文章主要給大家介紹了關(guān)于Android中實(shí)現(xiàn)WebView和JavaScript的互相調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來(lái)一起看看吧。2018-03-03

