亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何接收短信?

如何接收短信?

皈依舞 2021-05-10 14:34:35
我正在嘗試在Android應用程序上接收傳入的SMS,但是我無法收到即將收到的SMS,下面是正在使用的代碼。import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.Environment;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.util.Log;import android.widget.Toast;public class IncomingSms extends BroadcastReceiver {    // Get the object of SmsManager    final SmsManager sms = SmsManager.getDefault();    public void onReceive(Context context, Intent intent) {        // Retrieves a map of extended data from the intent.        final Bundle bundle = intent.getExtras();        try {            if (bundle != null) {                final Object[] pdusObj = (Object[]) bundle.get("pdus");                for (int i = 0; i < pdusObj.length; i++) {                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();                    String senderNum = phoneNumber;                    String message = currentMessage.getDisplayMessageBody();                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);                    // Show Alert                    int duration = Toast.LENGTH_LONG;                    Toast toast = Toast.makeText(context,                            "senderNum: "+ senderNum + ", message: " + message, duration);                    toast.show();                } // end for loop            } // bundle is null        } catch (Exception e) {            Log.e("SmsReceiver", "Exception smsReceiver" +e);        }    }}我嘗試在網上查找一些示例,但似乎沒有一個適合我,請指出問題出在哪里。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號