5

我想以编程方式在 android 中读取新传入 SMS 的消息正文。我尝试了一些东西,但没有返回任何内容:

我的代码有什么错误吗?

btnSendSms=(Button)findViewById(R.id.button1_SendSms);
            btnSendSms.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    String strCellNo1 , strCellNo2 , strCellNo3 , strCellNo4 , strCellNo5; 
                    SharedPreferences sendsmsSharedPref = Custom_Title_Bar_Activity.this.getSharedPreferences("MyPref",0);

                    strCellNo1 = sendsmsSharedPref.getString("num1", "n/a");
                    strCellNo2 = sendsmsSharedPref.getString("num2", "n/a");
                    strCellNo3 = sendsmsSharedPref.getString("num3", "n/a");
                    strCellNo4 = sendsmsSharedPref.getString("num4", "n/a");
                    strCellNo5 = sendsmsSharedPref.getString("num5", "n/a");

                    Log.e("Cell No One","->"+strCellNo1);
                    Log.e("Cell No Two","->"+strCellNo2);
                    Log.e("Cell No Three","->"+strCellNo3);
                    Log.e("Cell No Four","->"+strCellNo4);
                    Log.e("Cell No Five","->"+strCellNo5);

                    strPhones=strCellNo1;

                    if(strCellNo2.toString().length() >1 )
                    {
                        strPhones=strPhones+";"+strCellNo2;

                        if(strCellNo3.toString().length() >1 )
                        {
                            strPhones=strPhones+";"+strCellNo3;

                            if(strCellNo4.toString().length() >1 )
                            {
                                strPhones=strPhones+";"+strCellNo4;

                                if(strCellNo5.toString().length() >1 )
                                {
                                    strPhones=strPhones+";"+strCellNo5;

                                }

                            }

                        }

                    }



                    String message="Android";
                    txtSms=(TextView)findViewById(R.id.txt_Sms);
                    txtSms.setText(message);
                    String strMessage=txtSms.getText().toString();

                    /*strPhones=strCellNo1+";"+strCellNo2+";"+strCellNo3+";"+strCellNo4+";"+strCellNo5;*/
                    Log.e("String Phone->",""+strPhones);
                    edtxt_PhNO=(TextView)findViewById(R.id.txt_PhNO);
                    edtxt_PhNO.setText(strPhones);
                    String strPHNO=edtxt_PhNO.getText().toString();

                    StringTokenizer stokenizer=new StringTokenizer(strPHNO,";");
                    Log.e("",""+stokenizer);


                    while(stokenizer.hasMoreElements())
                    {
                        Log.e("Inside While Loop ","");
                        String tempMobileNumber=(String)stokenizer.nextElement();
                        Log.e("",""+tempMobileNumber);
                        if(tempMobileNumber.length() >0 && strMessage.trim().length() >0 )
                        {
                            sendSMS(tempMobileNumber, strMessage);
                            Log.e("sendSMS Method","");
                        }
                        else
                        {
                            Toast.makeText(getBaseContext(),"Please enter both phone number and message.",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });



    private void sendSMS(String phoneNumber, String message)
        {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

          //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            },new IntentFilter(SENT));

            //---when the SMS has been delivered---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(getBaseContext(), "SMS not delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;                        
                    }
                }
            }, new IntentFilter(DELIVERED));        

           /* SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       */


            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);  
        }

    and this is SmsReceiver class extends Broadcast Receiver


    public class SmsReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

             //---get the SMS message passed in---

            Bundle bundle = intent.getExtras();
            if (bundle != null) 
            {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];


                for (int i = 0; i < pdus.length; i++) 
                {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                StringBuffer content = new StringBuffer();
                if (messages.length > 0) 
                {
                    for (int i = 0; i < messages.length; i++) 
                    {
                        content.append(messages[i].getMessageBody());
                    }
                }


                String mySmsText = content.toString();
            }
        }
    }

谁能帮我这个?

4

1 回答 1

6

创建接收者喜欢

public void onReceiver(Context context, Intent intent){
    Object[] pdus=(Object[])intent.getExtras().get("pdus");
    SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);

    Log.d("SMSReceiver","SMS message sender: "+
       shortMessage.getOriginatingAddress());
    Log.d("SMSReceiver","SMS message text: "+
       shortMessage.getDisplayMessageBody());
  }

完整的例子在这里

http://droidcoders.blogspot.in/2011/09/sms-receive.html

于 2013-03-28T06:54:52.407 回答