Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive sms in android?

I am new to android and i am using android 2.1 for some sms_receive thing: when an sms received,it won't work... Nothing happens when sms received and I have force close, help!

Code for 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>

Class 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();
            }
        }
    }
}
like image 514
maryam_mo Avatar asked Dec 21 '22 01:12

maryam_mo


1 Answers

Your class name is wrong, according to your manifest file it should be like

 public class SmsReceiver extends BroadcastReceiver
like image 172
Deepanker Chaudhary Avatar answered Jan 02 '23 00:01

Deepanker Chaudhary