2

我想制作一个应用程序,它在 user1 中向 user2 发送带有一些特定文本的消息。用户 2 自动通过短信将位置发送给用户 1,但不应通知用户 2。下面的 BroadcastReceiver 收到文本时显示“知道了”.. 收到消息“UPDATE”时如何进行自动回复

IncomingSMSReciever.java

public class  IncomingSMSReceiver extends  BroadcastReceiver{
private static final String SMS_RECEIVED =
        "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context _context, Intent _intent) {

    if (_intent.getAction().equals(SMS_RECEIVED)) {
        Bundle bundle = _intent.getExtras();
        if (bundle != null) {


            Object[] pdus = (Object[]) bundle.get("pdus");

            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++)
                messages[i] = SmsMessage
                        .createFromPdu((byte[]) pdus[i]);
            for (SmsMessage message : messages) {
                String strPhoneNo = message.getOriginatingAddress();
                String msg = message.getMessageBody();

                if (msg.startsWith("UPDATE"))
                {

                    Toast.makeText(_context, "Whats up!!", Toast.LENGTH_LONG).show();
                    // this stops notifications to others
                    this.abortBroadcast();
                    // do what you want

                }
            }
        }
    }
}
}

SvrSmsListener.java

public class SrvSmsListener extends Service 
{
IncomingSMSReceiver SMSReceiver=new IncomingSMSReceiver();
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
IntentFilter filter = new IntentFilter(SMS_RECEIVED);

BroadcastReceiver receiver = SMSReceiver;
registerReceiver(receiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
if (SMSReceiver != null)
{
    unregisterReceiver(SMSReceiver);
}
}
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

}
}
4

1 回答 1

0

当您检测到收到的消息以“UPDATE”字开头时,您可以调用SmsManager 并使用sendTextMesage 向指定号码发送短信。您似乎已准备好除发送部分之外的所有代码,您是否面临问题。

于 2012-04-12T15:02:31.557 回答