0

如果我向某人发送一条短信,上面写着“更新”。所以我可以得到那个人的位置。我将制作一个广播接收器,它会在任何短信到达时处于活动状态。如果它与 UPDATE 匹配,则将发送其他用户的位置。

广播接收器。

public void onReceive(Context context, Intent intent) 
{   
//this stops notifications to others
this.abortBroadcast();

//---get the SMS message passed in---
Bundle bundle = intent.getExtras();   
SmsMessage[] msgs = null;
String str = "";            
if (bundle != null)
{
//---retrieve the SMS message received---
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]);                
    str += "SMS from " + msgs[i].getOriginatingAddress();
    from = msgs[i].getOriginatingAddress();
    str += " :";
    str += msgs[i].getMessageBody().toString();
    msg = msgs[i].getMessageBody().toString();
    str += "\n"; 
}
if(checksomething){
    //make your actions
    //and no alert notification and sms not in inbox
}
else{
    //continue the normal process of sms and will get alert and reaches inbox
    this.clearAbortBroadcast();
}
  }
4

1 回答 1

0

您需要获取短信的服务吗????

如果是

在清单中:

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

和您的服务:

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SrvSmsListener extends Service {

    private BroadcastReceiver IncomingSMSReceiver = new 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"))
                        {
                            // this stops notifications to others
                            this.abortBroadcast();
                            // do what you want
                        }
                    }
                }
            }
        }
    };

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
        IntentFilter filter = new IntentFilter(SMS_RECEIVED);
        BroadcastReceiver receiver = IncomingSMSReceiver;
        registerReceiver(receiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (IncomingSMSReceiver != null)
        {
            unregisterReceiver(IncomingSMSReceiver);
        }
    }
}
于 2012-04-07T12:23:16.193 回答