2

我编写了一个程序来在 Android 中发送和接收短信。发送短信成功,但当目标手机收到短信时,会显示强制关闭对话框。点击后打开收到的短信。

我希望收到的短信显示在我放入页面的文本视图中。

短信接收器.java

public class SmsReceiver extends BroadcastReceiver
    {
        public String str = "";
         @Override
            public void onReceive(Context context, Intent intent) 
            {
                //---get the SMS message passed in---
                Bundle bundle = intent.getExtras();        
                SmsMessage[] msgs = null;

                if (bundle != null)
                {
                     //Object[] smsExtra = (Object[]) bundle.getSMS_EXTRA_NAME);
                    //---retrieve the SMS message received---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length]; 
                    //for put sms in database---------------------
                   // ContentResolver contentResolver = context.getContentResolver();
                    //End for put sms in database---------------------
                    for (int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);    
                       // SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[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();
                   Intent act=new  Intent(context,MainActivity.class);
                   act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   act.putExtra("message",str);
                   context.startActivity(act);

                }                  
            }
        }

MainActivity.java

public class MainActivity extends Activity {
SmsReceiver _smsReceiver =new SmsReceiver();
private TextView showSms;
private String ReceivedSms;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // _smsReceiver.onReceive(getBaseContext(), getIntent());
        setContentView(R.layout.activity_main);
        Intent _intent =getIntent();
        ReceivedSms=_intent.getStringExtra("message");
          showSms=(TextView)this.findViewById(R.id.txt_Show);
          showSms.setText(ReceivedSms);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
4

1 回答 1

0

尝试测试这段代码,我已经根据您的代码实现了一个示例项目代码:

public class MainActivity extends Activity {
    private TextView showSms;
    private String   ReceivedSms;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        super.onCreate(savedInstanceState);
        // _smsReceiver.onReceive(getBaseContext(), getIntent());
        setContentView(R.layout.activity_main);
        Intent _intent = getIntent();
        ReceivedSms = _intent.getStringExtra("message");
        showSms = (TextView) this.findViewById(R.id.txt_Show);
        showSms.setText(ReceivedSms);

    }


}

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

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null) {
            // Object[] smsExtra = (Object[]) bundle.getSMS_EXTRA_NAME);
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            // for put sms in database---------------------
            // ContentResolver contentResolver = context.getContentResolver();
            // End for put sms in database---------------------
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                // SmsMessage sms =
                // SmsMessage.createFromPdu((byte[])smsExtra[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();
            Intent act = new Intent(context, MainActivity.class);
            act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            act.putExtra("message", str);
            context.startActivity(act);

        }
    }

在清单上确保添加权限:

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

android接收器声明:

        <receiver
            android:name="com.example.sms.SmsReceiver"
            class="com.example.sms.SmsReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

PS:删除SmsReceiver _smsReceiver =new SmsReceiver(); 从你的活动。

于 2012-10-24T08:46:48.750 回答