-1

是否可以读取传入的短信并仅回复特定或所需号码。即我想制作将在后台运行的应用程序......每当我用我的号码发送短信时,它会自动回复我的 XYZ 信息

4

1 回答 1

4

是的,这是可能的。您需要BroadcastReceiver在您的应用程序中添加一个拦截传入的 SMS 消息,然后如果它与您要查找的号码匹配则发送一条消息。

以下源代码来自:
BroadcastReceiver + SMS_RECEIVED
http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/

在 AndroidManifest.xml 中:

<receiver android:name=".SMSReceiver"> 
  <intent-filter android:priority="999"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
  </intent-filter> 
</receiver>

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

在名为 SMSReceiver.java 的新文件中:

public class SMSReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        // get SMS data, if bundle is null then there is no data so return
        Bundle extras = intent.getExtras();
        if (extras == null) return;

        // extract SMS data from bundle
        Object[] pdus = (Object[]) extras.get("pdus");
        for (int i = 0; i &lt; pdus.length; i++) {
            SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String sender = SMessage.getOriginatingAddress();
            String body = SMessage.getMessageBody().toString();

            // if there's an SMS from this number then send
            if(sender.equals("+1800555555") {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(sender, null, "sms received", null, null);
            }
        }
    }
}
于 2013-07-25T21:35:59.470 回答