• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Receive sms on my app from specific number

Agili

Lurker
Jun 6, 2023
2
0
if (checkPermission()) {
btn1.setEnabled(true);
} else
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
}
btn1.setOnLongClickListener(this);
btn2.setOnLongClickListener(this);
btn3.setOnLongClickListener(this);
btn4.setOnLongClickListener(this);
btnState.setOnLongClickListener(this);
btnVanne.setOnLongClickListener(this);
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(messageReceiver);
}

public void login(View view) {
}

class MessageReceiver extends BroadcastReceiver {
public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
private static final String SMS_SENDER = "0000000000000";
@override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction() != null &&
ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++) {
messages = SmsMessage.createFromPdu((byte[]) pduArray);
}
String sms_from = messages[0].getDisplayOriginatingAddress();
if (sms_from.equals(SMS_SENDER)) {
StringBuilder bodyText = new StringBuilder();
for (SmsMessage message : messages) {
bodyText.append(message.getMessageBody());
}
String body = bodyText.toString();
ReceivedSms.setText(body);
abortBroadcast();
}
}

}
}
private boolean checkPermission() {
return false;
}
 
Last edited:
I am not a lurker I only mischoose the forum section cose its my first visit to this page sorry.
No worries, that label is based on your post count. Welcome to the forum. We don't have as many active devs as some other places, but everyone here is pretty friendly and helpful when we can be. :)
 
  • Like
Reactions: Agili
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones