Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果sms内容匹配,则获取某人的位置_Android_Sms_Broadcastreceiver_Latitude Longitude - Fatal编程技术网

Android 如果sms内容匹配,则获取某人的位置

Android 如果sms内容匹配,则获取某人的位置,android,sms,broadcastreceiver,latitude-longitude,Android,Sms,Broadcastreceiver,Latitude Longitude,如果我给某人发短信说“更新”。这样我就能知道那个人的位置。我会做一个广播接收器,当任何短信到达时它都会被激活。如果与更新匹配,则将发送其他用户的位置 广播接收机 public void onReceive(Context context, Intent intent) { //this stops notifications to others this.abortBroadcast(); //---get the SMS message passed in--- Bundle bun

如果我给某人发短信说“更新”。这样我就能知道那个人的位置。我会做一个广播接收器,当任何短信到达时它都会被激活。如果与更新匹配,则将发送其他用户的位置

广播接收机

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();
}
  }
public void onReceive(上下文、意图)
{   
//这将停止向其他人发送通知
这个.abortBroadcast();
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于(inti=0;i您需要服务来获取您的短信吗

如果有的话

在舱单中:

<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);
        }
    }
}
导入android.app.Service;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类SrvSmsListener扩展服务{
private BroadcastReceiver IncomingSMSReceiver=新的BroadcastReceiver(){
收到专用静态最终字符串SMS_=
“android.provider.Telephony.SMS_已接收”;
@凌驾
接收时公共无效(上下文_上下文,意图_意图){
如果(_intent.getAction().equals(SMS_已接收)){
Bundle Bundle=_intent.getExtras();
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
SmsMessage[]messages=新SmsMessage[pdus.length];
对于(int i=0;i
您有问题吗?您没有问任何问题。我的问题是如何匹配sms内容(希望您了解我的意思是内容),如果它匹配UPDATE,则应接收您发送sms的人的位置此示例在设备接收到的sms以“UPDATE”开头时通知您如果内容等于UPDATE,我如何设置一个条件。
if(msg.compareTo(“UPDATE”)==0{//…}
只需获取消息正文并扫描子字符串“UPDATE”。(我会对消息中的值使用比“UPDATE”更独特的内容。)您可以使用if(!(yourString.indexOf(“UPDATE”)<0)){do your thing};