如何在Android中接收短信?

时间:2013-08-28 06:11:23

标签: android broadcastreceiver

我是Android的新手,我正在使用android 2.1进行一些sms_receive的事情:当收到短信时,它将无法正常工作......当收到短信时我没有任何反应,我有强制关闭,帮助!

androidmanifest.xml的代码:

<uses-sdk android:minsdkversion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:label="@string/app_name">
    <activity>
        android:name=".SmsReceiver"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.example.smsreceiver.SmsReceiver" android:enabled="true">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

类RSMSActivity:

public class RSMSActivity extends BroadcastReceiver {

    private static final String SHORTCODE = "55443";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        // Create messages for each incoming PDU
        for (int n = 0; n < messages.length; n++) {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        for (SmsMessage msg : sms) {
            // Verify if the message came from our known sender
            if (TextUtils.equals(msg.getOriginatingAddress(), "09358921973")) {
                Toast.makeText(context, "Received message from the mothership: " + msg.getMessageBody(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:7)

您的班级名称错误,根据您的清单文件,它应该是

 public class SmsReceiver extends BroadcastReceiver

答案 1 :(得分:4)

将您的班级名称从 RSMSActivity 更改为 SmsReceiver
检查一下: