0

我正在制作扩展广播接收器的二等 SMSReciever。在那我写代码但是在活动类中写什么?

4

2 回答 2

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" />
于 2012-06-20T09:17:14.783 回答
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();
    }
}
于 2012-06-20T09:00:36.540 回答