如何使用BroadcastReceiver接收传入的SMS

时间:2012-06-20 08:57:10

标签: android

我正在制作二级SMSReciever,它扩展了Broadcast Reciever。在那里我编写代码,但在活动类中写什么。 请帮助我新手使用android。

非常感谢。

2 个答案:

答案 0 :(得分:0)

以下是在Activity

中编写的代码
    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final Bundle bundle = intent.getExtras();        
            String str = "";            
            if (bundle != null) {
                //---retrieve the recent SMS message received---
                final Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] msgs = new SmsMessage[pdus.length];

                msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]);
                str += msgs[0].getMessageBody().toString();
                //---display the new SMS message---

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        final TextView smsContent = (TextView) findViewById(R.id.smsContent);
                        smsContent.setText("Message : " + msgs[0].getMessageBody().toString());

                        final TextView smsFrom = (TextView) findViewById(R.id.smsFrom);
                        smsFrom.setText("SMS from : " + msgs[0].getOriginatingAddress());
                    }
                });
            }
        }
    };
    registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

这是SMSReceiver类的代码

@Override
public void onReceive(Context context, Intent intent)  {
    //---get the SMS message passed in---
    final Bundle bundle = intent.getExtras();
    String str = "";

    if (bundle != null) {

        //---retrieve the recent SMS message received---
        final Object[] pdus = (Object[]) bundle.get("pdus");
        final SmsMessage[] msgs = new SmsMessage[pdus.length];

        msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]);
        str += "SMS from " + msgs[0].getOriginatingAddress();
        str += " :";
        str += msgs[0].getMessageBody().toString();
        str += "\n";

        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
    }
}

答案 1 :(得分:0)

请将以下代码写入您的广播接收器类,此处不需要启动器活动。

@Override
public void onReceive(Context context, Intent intent) {
     //---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();                     
              str += " :";
              str += msgs[i].getMessageBody().toString();
              str += "\n";        
         }
         //---display the new SMS message---
         Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
     }       

}

将以下代码写入清单文件。

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <receiver android:name="IncomingSmsCaptureApp" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
</application>

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