如果短信内容匹配,获取某人的位置

问题描述 投票:0回答:1

如果我发送短信给“更新”的人。所以我可以得到那个人的位置。我将制作一个广播接收器,当任何短信到达时它将被激活。如果它与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();
}
  }
android sms broadcastreceiver latitude-longitude
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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.