收听短信并在Android中自动回复

收听短信并在Android中自动回复

本文介绍了收听短信并在Android中自动回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何在android中自动回复收到的消息?

How to reply to an incoming message automatically in android?

这是我的IncomingSMS类中的我的onReceive()方法,该方法扩展了BroadcastRecevier,它的问题是它发送许多消息,我希望它仅发送一个SMS答复.

Here's my onReceive() method from my IncomingSMS class which extends BroadcastRecevier, problem with it is that it sends many messages, I want it to send only one SMS reply.

public class IncomingSMS extends BroadcastReceiver{
private String senderNumber;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;

    String message = "";

    if(bundle != null){
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];



        for(int i = 0; i < msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            message = msgs[i].getMessageBody();
            senderNumber = msgs[i].getOriginatingAddress();
        }
        senderNumber = msgs[0].getOriginatingAddress();

        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        sendMessage(senderNumber, "Thank you for sending me a message");
    }
}

private void sendMessage(String number, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, message, null, null);
}

推荐答案

您所面对的是正常行为.由于您正在侦听所有传入的SMS,因此它必然会在收到谢谢"消息时再次发送出SMS.您可以做的是,发送一个在SMS中唯一的值或布尔值,然后进行检查.如果该值存在,则根本就不会再发送该消息.

What you are facing is the normal behavior. Since you are listening for all incoming SMS, it is bound to send out a SMS again when it receives the thank you message. What you can do is, send a value or a boolean which is unique in the SMS and put a check for it. If the value exists you simply don't send the message again.

这篇关于收听短信并在Android中自动回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:03