How to get sms in Android?

I am new to android and I use Android 2.1 for some sms_receive things: when sms received, it will not work ... Nothing happens when sms received and I have the power, 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> 

RSMSActivity Class:

 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(); } } } } 
+4
source share
2 answers

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

  public class SmsReceiver extends BroadcastReceiver 
+7
source

Change the name of your RSMSActivity class to SmsReceiver .
Check it out again:

+4
source

Source: https://habr.com/ru/post/1499299/


All Articles