如何在Android中自動回覆收到的消息?在Android中收聽短信並自動回覆它
這是我的IncomingSMS類中的onReceive()方法,它擴展了BroadcastRecevier,問題在於它發送很多消息,我希望它只發送一條SMS回覆。
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);
}
你是否發送給另一個號碼來測試它? – user2511882
是的,我希望它只發送一個短信給該號碼(誰最初發送消息),(它是應用程序的一部分,將安裝在設備上,發送者和接收者) – anshulv