Android接收和發(fā)送短信處理
關(guān)于短信接收處理方面,當(dāng)前已經(jīng)有一些app做的比較好了,比如發(fā)給手機(jī)發(fā)驗(yàn)證碼驗(yàn)證的問題,很多app在手機(jī)接收到驗(yàn)證碼后,不需要輸入,就直接可以跳過驗(yàn)證界面,這就是用到了對接收到的短信的處理。至于短信的發(fā)送,也沒什么好說的了。在此也只是附上一個(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)聽的廣播 receiveFilter = new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); messageReceiver = new MessageReceiver(); registerReceiver(messageReceiver, receiveFilter); //為發(fā)送短信設(shè)置要監(jiān)聽的廣播 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ù)對短信的發(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)聽 unregisterReceiver(messageReceiver); unregisterReceiver(sendStatusReceiver); } class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); //使用pdu秘鑰來提取一個(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ā)送方號碼 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- android中可以通過兩種方式調(diào)用接口發(fā)送短信
- Android Mms之:短信發(fā)送流程(圖文詳解)
- Android實(shí)現(xiàn)將已發(fā)送的短信寫入短信數(shù)據(jù)庫的方法
- Android發(fā)送短信功能代碼
- Android短信發(fā)送器實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)讀取手機(jī)聯(lián)系人、撥號、發(fā)送短信及長按菜單操作方法實(shí)例小結(jié)
- Android實(shí)現(xiàn)短信發(fā)送功能
- Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- Android基礎(chǔ)開發(fā)小案例之短信發(fā)送器
相關(guān)文章
Android使用Profiler查看應(yīng)用內(nèi)存分析的操作步驟
內(nèi)存分析是Profiler中的一個(gè)組件,可以幫助我們識(shí)別可能會(huì)導(dǎo)致應(yīng)用卡頓、凍結(jié)甚至崩潰的內(nèi)存泄露和內(nèi)存抖動(dòng),本文小編將給大家介紹一下Android使用Profiler查看應(yīng)用內(nèi)存分析的操作步驟,需要的朋友可以參考下2023-10-10Android App開發(fā)中ViewPager組件的入門使用教程
這篇文章主要介紹了Android App開發(fā)中ViewPager組件的入門使用教程,ViewPager主要用來實(shí)現(xiàn)通過滑動(dòng)來切換頁面的效果,需要的朋友可以參考下2016-03-03Android Studio配合WampServer完成本地Web服務(wù)器訪問的問題
這篇文章主要介紹了Android Studio配合WampServer完成本地Web服務(wù)器訪問,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Android簡單實(shí)現(xiàn)動(dòng)態(tài)權(quán)限獲取相機(jī)權(quán)限及存儲(chǔ)空間等多權(quán)限
這篇文章主要介紹了Android簡單實(shí)現(xiàn)動(dòng)態(tài)權(quán)限獲取相機(jī)權(quán)限及存儲(chǔ)空間等多權(quán)限,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07Android應(yīng)用閃屏頁延遲跳轉(zhuǎn)的三種寫法
這篇文章主要介紹了 Android應(yīng)用閃屏頁延遲跳轉(zhuǎn)的三種寫法,需要的朋友可以參考下2017-03-03android studio 的下拉菜單Spinner使用詳解
這篇文章主要介紹了android studio 的下拉菜單Spinner使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12