如何从特殊电话号码接收短信?

时间:2012-10-28 08:41:46

标签: android android-intent android-emulator sms broadcastreceiver

我在android中成功写了一个发送和接收短信。

我希望我的程序能够从特殊号码(“+ 9856874236”)接收短信。但是,如果短信来自任何其他号码,它应该转到手机的留言收件箱,而不是我的应用程序。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

    public class SmsReceiver extends BroadcastReceiver {
        public String str = "";

        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle bundle = intent.getExtras();
            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage[] msgs = null;

            if (bundle != null)
            {

                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];      
                String msg_from="";
                for (int n = 0; n < messages.length; n++) 
                {
                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
                      msg_from += msgs[n].getOriginatingAddress();
                }

                String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();


                if(msg_from .equals("+989124236870"))  
                {
                for (int n = 0; n < messages.length; n++) 
                {

                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]); 
                    str += "SMS from " + msgs[n].getOriginatingAddress();
                    str += " :";
                    //str += "sms az shomare makhsus";
                    str += msgs[n].getMessageBody().toString();

                    str += "\n";

               abortBroadcast();

               }


                Intent act = new Intent(context, MainActivity.class);
                act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                act.putExtra("message", str);
                context.startActivity(act);
            }


            }

        }
    }

3 个答案:

答案 0 :(得分:0)

为什么要在for循环中附加msg_from参数?

msg_from += msgs[n].getOriginatingAddress();

不应该只是:

msg_from[n] = msgs[n].getOriginatingAddress();

基本上有一个msg_from [n]的数组,并为每个消息放入每个原始地址。 然后通过循环访问msg_from数组来检查每条消息?

您可以按如下方式在for循环内移动原点检查:

for (int n = 0; n < messages.length; n++) {
           if(msg_from[n].equals("+9856874236")){  
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]); 
                str += "SMS from " + msg_from[n];
                str += " :";
                //str += "sms az shomare makhsus";
                str += msgs[n].getMessageBody().toString();

                str += "\n";

                 abortBroadcast();
               }else{
               //Do something else 
               }
           }

答案 1 :(得分:0)

使用此示例并对其进行自定义:

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("my text"))
                        {
                            // this stops notifications to others
                            this.abortBroadcast();

                        }
                    }
                }
            }
        }
    };

    @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);
        }
    }
}

答案 2 :(得分:0)

我也有这个问题。现在我使用这个代码并且它正常工作:

        `enter code here`  
         if ( extras != null ) {
        // get array data from SMS
        Object[] smsExtra = (Object[]) extras.get( "pdus" ); // "pdus" is the key

        for ( int i = 0; i < smsExtra.length; ++i ) {
            // get sms message
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
            // get content and number
            String body = sms.getMessageBody();
            String address = sms.getOriginatingAddress();
            // create display message

            if( address.contains("5558")){

            messages += "SMS from " + address + " :\n";
            messages += body + "\n";

       // notify new arriving message
            Toast.makeText( context, messages, Toast.LENGTH_LONG ).show();

        this.abortBroadcast();

    }
    }

并且也不要忘记将此代码用于清单:

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

 .....
<receiver android:name=".SmsReceiver" android:exported="true" >
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />

</intent-filter>
</receiver>

此程序仅从port =“5558”(在模拟器中)接收并将其他信息存储在您的收件箱中。

是(09124236870)你自己的电话号码吗?!!!